Java 为什么EntityManager在spring事务中为空?
我在java webapp项目的服务部分编写了以下代码:Java 为什么EntityManager在spring事务中为空?,java,spring,spring-mvc,entitymanager,spring-transactions,Java,Spring,Spring Mvc,Entitymanager,Spring Transactions,我在java webapp项目的服务部分编写了以下代码: @Service @Transactional public class PersonService extends BaseService { @PersistenceContext private EntityManager entityManager; public void insert(Person person) { entityManager.persist(person);
@Service
@Transactional
public class PersonService extends BaseService {
@PersistenceContext
private EntityManager entityManager;
public void insert(Person person) {
entityManager.persist(person);
}
public boolean checkUserName(String userName) {
try {
System.out.println("Entity null ni");
Person person = (Person) entityManager.createQuery("select entity from person entity where entity.USER_NAME=:userName")
.setParameter("userName", userName)
.getSingleResult();
if (person == null)
return true;
else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
当我尝试在不使用checkUserName方法的情况下插入新记录时,在这种情况下,记录将正确插入,entityManager不为null,但当我想检查重复用户名时,我得到了NullPointerException。我检查了代码中可能为null的每个对象。最后我了解entityManager为null。为什么entityManager在Spring事务中为null。这是Spring.xml
<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:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!--SpringMVC-->
<context:component-scan base-package="Laitec.Doctor"/>
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
</bean>
<!-- SpringTransaction-->
<bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="Laitec.Doctor.model.entity"/><!--ToPackageAddress-->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!--<prop key="hibernate.hbm2ddl.auto">create-drop</prop>-->
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
<property name="username" value="username"/>
<property name="password" value="password"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryBean"/>
</bean>
<tx:annotation-driven/>
</beans>
也许你正在实例化PersonService调用新的PersonService?在这种情况下,Spring没有执行任何操作,因此字段“entityManager”为空。必须从应用程序上下文中获取bean
applicationContext.getBean(PersonService.class);
或者从测试或控制器调用它
@RestController
@GetMapping("people")
public class PersonController() {
@Autowired
private PersonService personService;
@PostMapping
public void home(@RequestBody Person person) {
personService.insert(person);
}
}
可能是因为您使用new创建了服务实例。但是如果没有异常和相关代码的完整堆栈跟踪,就不可能说了。我也遇到了类似的问题。在我的例子中,一个方法被标记为final,EntityManager在final方法中始终为null,但只有在使用@Transactional注释时才为null。你是对的,我在控制器中使用Autowired for personService,并在其中使用insert方法,因此它工作正常,但在另一个类中使用checkUserName方法并创建了一个personService的新实例,因此导致nullPointerException。我不知道您在哪里使用checkUserName,但您可以创建一个标记为@Component的帮助器或util类,然后在希望的范围内使用它。另一种选择是使用IoC by constructor,这是当今推荐的:baeldung.com/constructor-injection-in-springer。发布类似的命令是因为我用new关键字修改了实例服务,这就是EntityManager为null的原因。