How to inject EJB3 in spring mvc?

3

I would like to know how to integrate ejb3 with spring mvc. I tried to do it by writing @EJB on the controller using the lookup attribute of the @EJB annotation and it still does not work. Thank you very much for replying, greetings.

business interface that implements the ejb

 package modelo;

import javax.ejb.Local;
@Local
public interface DaoUsuario {
    boolean loguearUsuario(String user,String pw);
}

ejb of stateless type that implements the DaoUsuario interface

package modelo.ejb;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.sql.DataSource;
import modelo.DaoUsuario;

@Stateless
@LocalBean

public class DaoUsuarioEjb implements DaoUsuario {

@Resource(mappedName="jdbc/formacionds",type=javax.sql.DataSource.class)
DataSource ds;

@Override
public boolean loguearUsuario(String user, String pw) {
    PreparedStatement st = null;
    boolean resultado = false;
    try {
    st = ds.getConnection().prepareStatement("SELECT count(*) as 'resultado' FROM alumnos where usuario = ? and password = ?");
    st.setString(1, user);
    st.setString(2, pw);
    int coincidencias = st.executeQuery().getInt("resultado");

        if(coincidencias == 1) {
            resultado = true;
        } 

        st.close();

    } catch(SQLException se) {
        try {
            st.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    return resultado;
}

}

Finally the driver made with spring mvc

package controller;

import javax.ejb.EJB;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import modelo.DaoUsuario;

@Controller
@RequestMapping(path="/validacion")
public class loginController {

@EJB(lookup="java:app/modelo.ejb/DaoUsuarioEjb")
DaoUsuario usu;

@RequestMapping(path={"/login"}, method={RequestMethod.POST})
public String loguearUsuario(@RequestParam("usuario") String user,@RequestParam("password") String pw) {
    return usu.loguearUsuario(user, pw)?"bienvenida":"error";
}

}

As you can see in the controller in the notation ejb in the lookup attribute I have the path jndi (I do not know if it is what is wrong)

    
asked by Alberto Alaber 28.08.2018 в 11:44
source

0 answers