JPA的上下文注入

时间:2010-01-01 18:18:06

标签: java spring jpa persistence

我正在尝试使用JPA的Context Injection。目前我的代码无法注入上下文,抛出NullPointerException:

  

线程“main”中的异常   java.lang.NullPointerException at   learn.spring.dao.ListingDao.findById(ListingDao.java:19)

以下是我的代码,任何人都会看到错误/遗漏?感谢。

Spring配置xml:

<beans ....>

<tx:annotation-driven />
<context:annotation-config />
<aop:aspectj-autoproxy />

<bean id="listingDao" class="learn.spring.dao.ListingDao" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName" value="com.mysql.jdbc.Driver" />
 <property name="url" value="jdbc:mysql://xxx.xxx.xxx.xxx/xxxxx" />
 <property name="username" value="xxxxxx" />
 <property name="password" value="xxxxxx" />
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
 <property name="persistenceUnitName" value="learnSpring" />
 <property name="dataSource" ref="dataSource" />
 <property name="jpaVendorAdapter">
  <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
   <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
   <property name="showSql" value="true" />
   <property name="generateDdl" value="true" />
  </bean>
 </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
 <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
...
</beans>

DAO课程:

package learn.spring.dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Transactional;

import learn.spring.model.Registration;

@Transactional
public class ListingDao {
 public ListingDao() {}

 @PersistenceContext
 private EntityManager entityManager;

 public Registration findById(int id) {
  Query query = entityManager.createQuery("FROM Registration r WHERE r.memberId = ?1");
  query.setParameter(1, id);
  return (Registration) query.getSingleResult();
 }
}

peristence.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">

<persistence-unit name="learnSpring" />

</persistence>

类w /“主要”方法:

public class Performance {

 public static void main(String[] args){
  ApplicationContext context = new ClassPathXmlApplicationContext("learn-spring.xml");
  Performer performer = (Performer) context.getBean("musician");
  performer.perform();
  ListingDao listingDao = new ListingDao();
  Registration registration = listingDao.findById(1);
  System.out.println("Member id = " + registration.getMemberId() + " Name = " + registration.getFirstName() + " " + registration.getLastName());
 }

}

2 个答案:

答案 0 :(得分:2)

您的问题是您的ListingDao不属于春季背景。

您希望从spring的依赖注入中受益的所有类必须通过spring 实例化。当您使用ListingDao运算符实例化new时,您的对象不属于弹簧上下文,并且不会自动EntityManager

所以解决方案是从上下文中获取ListingDao,就像你对表演者所做的那样:

ListingDao listingDao = (ListingDao) context.getBean("listingDao");

作为旁注 - 避免直接访问spring上下文。即避免像上面那样的线。使用注释来注入依赖项,而不是请求它们,因为您正在将依赖注入框架转换为服务定位器。

答案 1 :(得分:1)

当你实例化ListingDAO时,你是在spring上下文之外进行的 - 所以spring永远不会有机会连接EntityManager。

你应该替换:

  ListingDao listingDao = new ListingDao();

使用:

  ListingDao listingDao = (ListingDao) context.getBean("listingDao");