gpt4 book ai didi

java - JPA 的上下文注入(inject)

转载 作者:行者123 更新时间:2023-12-04 07:00:16 25 4
gpt4 key购买 nike

我正在尝试使用 JPA 的上下文注入(inject)。目前我的代码未能注入(inject)上下文,抛出 NullPointerException:

Exception in thread "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>

带“main”方法的类:
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());
}

}

最佳答案

你的问题是你的 ListingDao不是 spring 上下文的一部分。

您希望从 spring 的依赖注入(inject)中受益的所有类都必须由 spring 实例化。当你实例化你的 ListingDaonew运算符,您的对象不是 spring 上下文的一部分,它不会收到自动 EntityManager .

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

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

作为旁注 - 避免直接访问 Spring 上下文。即避免像上面这样的行。使用注解来注入(inject)你的依赖,而不是请求它们,因为你因此将一个依赖注入(inject)框架变成了一个服务定位器。

关于java - JPA 的上下文注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1989213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com