Spring 弹簧靴2@注入@Autowired依赖项之前调用的Bean方法
这只是好奇。 在下面的示例中,@Autowired entityManager Factory和@Autowired ApplicationContext在@Bean entityManager()方法之前被注入 但当我将EntityManager bean类型更改为SessionFactory时,会在自动关联EntityManager Factory和ApplicationContext bean之前调用SessionFactory()方法,从而在展开SessionFactory时导致NullPointerException。下面的代码片段Spring 弹簧靴2@注入@Autowired依赖项之前调用的Bean方法,spring,spring-boot,dependency-injection,configuration,autowired,Spring,Spring Boot,Dependency Injection,Configuration,Autowired,这只是好奇。 在下面的示例中,@Autowired entityManager Factory和@Autowired ApplicationContext在@Bean entityManager()方法之前被注入 但当我将EntityManager bean类型更改为SessionFactory时,会在自动关联EntityManager Factory和ApplicationContext bean之前调用SessionFactory()方法,从而在展开SessionFactory时导致Null
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Autowired
private EntityManagerFactory entityManagerFactory;
@Autowired
private ApplicationContext context;
@Bean
public SessionFactory sessionFactory() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
return entityManager.unwrap(SessionFactory.class);
}
}
我的问题是:为什么会这样 据我所知,有两种方法可以获得
会话工厂:
来自EntityManagerFactory
return entityManagerFactory.unwrap(SessionFactory.class)
//or -> if you have entitymanager
return em.getEntityManagerFactory().unwrap(SessionFactory.class);
Session session = entityManager.unwrap(Session.class);
return session.getSessionFactory();
来自会话
return entityManagerFactory.unwrap(SessionFactory.class)
//or -> if you have entitymanager
return em.getEntityManagerFactory().unwrap(SessionFactory.class);
Session session = entityManager.unwrap(Session.class);
return session.getSessionFactory();
你好奇的原因就像你说的
在自动关联EntityManagerFactory和ApplicationContext bean导致NullPointerException之前调用sessionFactory()方法
事实并非如此据我记忆,有两种方法可以获得会话工厂
:
来自EntityManagerFactory
return entityManagerFactory.unwrap(SessionFactory.class)
//or -> if you have entitymanager
return em.getEntityManagerFactory().unwrap(SessionFactory.class);
Session session = entityManager.unwrap(Session.class);
return session.getSessionFactory();
来自会话
return entityManagerFactory.unwrap(SessionFactory.class)
//or -> if you have entitymanager
return em.getEntityManagerFactory().unwrap(SessionFactory.class);
Session session = entityManager.unwrap(Session.class);
return session.getSessionFactory();
你好奇的原因就像你说的
在自动关联EntityManagerFactory和ApplicationContext bean导致NullPointerException之前调用sessionFactory()方法
情况并非如此从Hibernate 5.2开始,它也是一个实体管理工厂
,因为它现在扩展了上述接口。在此之前,我们正在包装一个EntityManagerFactory
因此,EntityManagerFactory
无法注入,因为SessionFactory
是实现该接口的实际bean。从Hibernate 5.2开始,它也是一个EntityManagerFactory
,因为它现在扩展了所述接口。在此之前,我们正在包装一个EntityManagerFactory
因此,无法注入EntityManagerFactory
,因为SessionFactory
是实现该接口的实际bean。我相信您做得不对。。你能像这样展开会话吗session=unwrap(session.class);然后获取session.getSessionFactory();谢谢你的回复。我不是问如何正确地做到这一点。我只是好奇为什么@Bean
定义在自动关联其他字段(即@Bean
类型为EntityManagerFactory)之后被调用,而在自动关联其他字段(即Bean类型为SessionFactory)之前被调用。我只是想知道这背后的原因,因为我自己找不到答案。我认为在自动连接其他字段之前不会调用它(@bean),因为这将违反java JVM。同样,JVM在初始化所有数据成员字段之前不允许任何方法调用。根据您的Hibernate版本,EntityManagerFactory
是SessionFactory
@M.Deinum是的,就是这样。我的第二个代码片段是在entityManager.unwrap(SessionFactory.class)中抛出NPE因为我现在正在定义SessionFactorybean和SessionFactory扩展EntityManagerFactory。这就是为什么在自动连接之前调用Bean定义。把你的评论转化为答案,我会把它标记为一个被接受的答案。我相信你做错了。。你能像这样展开会话吗session=unwrap(session.class);然后获取session.getSessionFactory();谢谢你的回复。我不是问如何正确地做到这一点。我只是好奇为什么@Bean
定义在自动关联其他字段(即@Bean
类型为EntityManagerFactory)之后被调用,而在自动关联其他字段(即Bean类型为SessionFactory)之前被调用。我只是想知道这背后的原因,因为我自己找不到答案。我认为在自动连接其他字段之前不会调用它(@bean),因为这将违反java JVM。同样,JVM在初始化所有数据成员字段之前不允许任何方法调用。根据您的Hibernate版本,EntityManagerFactory
是SessionFactory
@M.Deinum是的,就是这样。我的第二个代码片段是在entityManager.unwrap(SessionFactory.class)中抛出NPE因为我现在正在定义SessionFactorybean和SessionFactory扩展EntityManagerFactory。这就是为什么在自动连接之前调用Bean定义。将您的评论转换为答案,我会将其标记为已接受的答案