Bom dia pessoal. Estou iniciando os estudos com spring e não estou conseguindo fazera a injeção de dependência funcionar. Sempre recebo um NullPointerExpection quando chamo o método do meu Dao. Minhas aplicação é bem simples. Ela apenas persiste um objeto livro. Seguem as classes e arquivos de configuração
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>biblioteca</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--Configuração do spring-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--Servlet que faz o controle das requisições -->
<servlet>
<description></description>
<display-name>ServletController</display-name>
<servlet-name>ServletController</servlet-name>
<servlet-class>com.exemplo.biblioteca.controller.ServletController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletController</servlet-name>
<url-pattern>/app</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="livroDao" class="com.exemplo.biblioteca.dao.LivroDaoImpl"/>
<bean id="livroBean" class="com.exemplo.biblioteca.bean.LivroBean">
<property name="livroDao" ref="livroDao"/>
</bean>
</beans>
LivroBean, que é a classe na qual deveria ser injetado o meu Dao
package com.exemplo.biblioteca.bean;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.exemplo.biblioteca.dao.LivroDao;
import com.exemplo.biblioteca.dominio.Livro;
import com.exemplo.biblioteca.logica.Logica;
public class LivroBean implements Logica {
private LivroDao livroDao;
public LivroDao getLivroDao() {
return livroDao;
}
public void setLivroDao(LivroDao livroDao) {
this.livroDao = livroDao;
}
@Override
public void executa(HttpServletRequest request, HttpServletResponse response)
throws Exception {
Livro livro = new Livro();
livro.setAutor(request.getParameter("autor"));
livro.setTitulo(request.getParameter("titulo"));
livroDao.gravar(livro);
RequestDispatcher rd = request.getRequestDispatcher("sucesso.jsp");
rd.forward(request, response);
}
}
package com.exemplo.biblioteca.dao;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import com.exemplo.biblioteca.dominio.Livro;
public class LivroDaoImpl implements LivroDao {
private EntityManagerFactory emf = Persistence.createEntityManagerFactory("biblioteca");
private EntityManager em = emf.createEntityManager();
@Override
public void gravar(Livro livro) {
EntityTransaction tx = this.em.getTransaction();
tx.begin();
em.persist(livro);
tx.commit();
em.close();
}
}
Agradeço ajuda.