我正在尝试使用Java Spring和hibernate构建一个应用程序。 对于dao类我正在使用泛型dao模式。当我尝试执行查询时,抛出nullpointer异常,因为entityManager为null。我认为它没有正确注入,但我不知道为什么。
申请背景:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean id = "klantDao" class="service.JpaKlantDao"/>
<bean id="userDao" class="service.JpaUserDao"/>
<bean id="assembler" class="service.Assembler" />
</beans>
Dispatcher servlet:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="controllers"/>
<context:component-scan base-package="service"/>
<mvc:resources mapping="/resources/**" location="/resources/" />
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/CentricJavaDB"/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>domain</value>
</list>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
</bean>
</property>
</bean>
<bean id="transactionManager"
class=" org.springframework.orm.jpa.JpaTransactionManager ">
<constructor-arg ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="mymessages" />
</bean>
</beans>
Web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml,
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<!--Spring Security Configuration-->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>overzicht.jsp</welcome-file>
</welcome-file-list>
</web-app>
我注入dao的控制器:
@Controller
public class LoginController {
@Autowired
private KlantDao klantDao;
@RequestMapping(value="/login", method = RequestMethod.GET)
public String klantInloggen(Model model)
{
String k;
k = klantDao.findAll().get(0).getEmailadres();
return "login";
}
}
通用dao界面:
public interface GenericDao<T> {
public List<T> findAll();
public T update(T object);
public T get(Long id);
public void delete(T object);
public void insert(T object);
public boolean exists(Long id) ;
}
通用dao实现:
@Transactional
public class JpaGenericDao<T> implements GenericDao<T> {
private Class<T> type;
private EntityManager em;
public JpaGenericDao(Class<T> type) {
super();
this.type = type;
}
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}
@Transactional(readOnly = true)
@Override
public T get(Long id) {
T entity = this.em.find(this.type, id);
return entity;
}
@Transactional(readOnly = true)
@Override
public List<T> findAll() {
return this.em.createQuery(
"select entity from " + this.type.getName() + " entity").getResultList();
}
//@Transactional
@Override
public void insert(T object) {
em.persist(object);
}
//@Transactional
@Override
public void delete(T object) {
em.remove(em.merge(object));
}
@Transactional(readOnly = true)
@Override
public boolean exists(Long id) {
T entity = this.em.find(this.type, id);
return entity != null;
}
@Override
public T update(T object) {
return em.merge(object);
}
}
Klant dao界面:
public interface KlantDao
{
public List<Klant> findAll();
}
klant dao实施:
public class JpaKlantDao extends JpaGenericDao<Klant> implements KlantDao
{
public JpaKlantDao()
{
super(Klant.class);
}
}
我希望有人可以帮助我,我已经调试了几个小时,我找不到我做错了什么。