Inject EJB in Bean Spring

Asked

Viewed 78 times

2

I’m working on a Java EE + Primefaces + JPA project with EJB. In the authentication part I decided to use Spring Security, but I’m having a hard time getting the EJB inside the Spring controller. I found some tutorials, but I did not succeed. If someone there has done something similar and can give me a light I would be very grateful.

Spring controller:

public class AppUserDetailsService implements UserDetailsService {

private UserEJBLocal repositorio;

@Override
public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException {
    Usuario usuario = repositorio.getUsuarioPorLogin(login);
    UsuarioSistema user = null;

    if(usuario != null) {
        user = new UsuarioSistema(usuario, getGrupos(usuario));
    }

    return user;
}

private Collection<? extends GrantedAuthority> getGrupos(Usuario usuario) {
    List<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>();

    for(Grupo grupo : usuario.getGrupos()) {
        authorities.add(new SimpleGrantedAuthority(grupo.getNome().toUpperCase()));
    }

    return authorities;
}

public UserEJBLocal getRepositorio() {
    return repositorio;
}

public void setRepositorio(UserEJBLocal repositorio) {
    this.repositorio = repositorio;
}

}

Ejbs:

public interface UserEJBLocal {
    public Usuario getUsuarioPorLogin(String login);
}


    @Stateless(mappedName = "ejb/userFind")
public class UserEJB implements UserEJBLocal {

    @PersistenceContext(unitName = "martanPU")
    private EntityManager em;

    @Override
    public Usuario getUsuarioPorLogin(String login) {
        Usuario usuario = null;

        try {
            usuario = getEntityManager().createQuery("SELECT user FROM Usuario AS user WHERE user.login = :login", Usuario.class)
                    .setParameter("login", login.toLowerCase()).getSingleResult();
        } catch (NoResultException e) {
            e.printStackTrace();
        }

        return usuario;
    }

    protected EntityManager getEntityManager() {
        return em;
    }
}

Configuration of EJB in Spring:

<beans:bean id = "userEJBLocal"
                class = "org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean" >
        <property name = "jndiName" value = "ejb/userFind" />
        <property name = "businessInterface" value = "br.com.icone.martan.modelo.repositorio.UserEJBLocal" />
    </beans:bean>

    <beans:bean id = "myController" class = "br.com.icone.martan.security.AppUserDetailsService" >
        <property name = "userEJBLocal" ref = "userEJBLocal" />
    </beans:bean>
  • I don’t use EJB, I prefer to use 100% Spring in my applications. I’ve seen a lot of people with this same problem you reported. Try these links I’ll give you: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/ejb.html | https://access.redhat.com/documentation/en-US/JBoss_Web_Framework_Kit/1.2/html/Spring_Developer_Guide/ch07s05.htmlhttp| ://shazsterblog.blogspot.com.br/2011/08/integrating-ejb3-Session-bean-with.html

  • I was able to solve it by annotating the Userejblocal interface with @Remote and lookup it like this: Initialcontext ctx = new Initialcontext(); repositorio = (Userejbremote) ctx.lookup("br.com.icone.martan.modelo.repositorio.Userejbremote");

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.