I created in Netbeans a login with hibernate (annotations) and access to MySql but when I added Spring (also with annotations) I got stuck, already in the first attempt Spring does not create the instance of an object (dependency injection) . I have created the packages classes, bean, bo, bo.Spring, dao, dao.Spring with the following code as it corresponds:
In the Classes package the code is:
package clases;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="usuario_login", catalog="sipreli")
public class UsuarioLogin implements java.io.Serializable {
//@Column(name="usuario", unique=true, nullable=false, length=20)
@Id
private String usuario;
// @Column(name="contrasena", length=20)
private String contrasena;
// sus respectivos métodos de geter y setter de las variables
In the bean package the code is:
package beans;
import clases.UsuarioLogin;
import bo.spring.IUsuarioBo;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class UsuarioBean implements Serializable{
private UsuarioLogin elUsuario;
private IUsuarioBo usuarioBo; // variable de interface
private List<UsuarioLogin> listaUsuario;
public UsuarioBean (){
elUsuario = new UsuarioLogin();
}
public String entrar(UsuarioLogin elUsuario){
boolean existe;
// --------- Aquí es donde me da error a la hora de crear la instancia de usuarioBo ------------
existe = usuarioBo.entrar(elUsuario);
if (existe) {
return "index.xhtml";
} else{
return "";
}
}
In the bo package
package bo;
import clases.UsuarioLogin;
import bo.spring.IUsuarioBo;
import dao.spring.IUsuarioDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UsuarioBo implements IUsuarioBo {
@Autowired
private IUsuarioDao usuarioDao;
@Override
public boolean entrar(UsuarioLogin elUsuario) {
return usuarioDao.entrar(elUsuario);
}
public void setUsuarioDao(IUsuarioDao usuarioDao) {
this.usuarioDao = usuarioDao;
}
}
In the bo.Spring package
package bo.spring;
import clases.UsuarioLogin;
public interface IUsuarioBo {
boolean entrar (UsuarioLogin elUsuario);
}
The application.Context file has the following code
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
">
<!-- <context:annotation-config/> -->
<context:component-scan base-package="cr.ac.icai.cursoslibres.javaavanzado.bo"/>
<bean id="usuarioBo" class="cr.ac.icai.cursoslibres.javaavanzado.bo.UsuarioBo" >
<property name="usuarioDao" ref="usuarioDao"/>
</bean>
<bean id="usuarioDao" class="cr.ac.icai.cursoslibres.javaavanzado.dao.UsuarioDao"/>
</beans>
The face-config file has the following code
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
</faces-config>
The web.xml file has the following code (at the end of it, the first thing I think is the same as always)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!-- debe de ser igual al nombre del archivo context-->
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
I suppose that the error (NullPointerException) is due to a theme of misuse of annotations (where and what annotations use) and the configuration file of Spring. I have done my homework and I have read tutorials, consulted videos but I can not see why Spring does not do the instantiation (dependency injection) of userBo in the enter method (see previous code).