Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 弹簧靴2@注入@Autowired依赖项之前调用的Bean方法_Spring_Spring Boot_Dependency Injection_Configuration_Autowired - Fatal编程技术网

Spring 弹簧靴2@注入@Autowired依赖项之前调用的Bean方法

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

这只是好奇。 在下面的示例中,@Autowired entityManager Factory和@Autowired ApplicationContext在@Bean entityManager()方法之前被注入

但当我将EntityManager bean类型更改为SessionFactory时,会在自动关联EntityManager Factory和ApplicationContext bean之前调用SessionFactory()方法,从而在展开SessionFactory时导致NullPointerException。下面的代码片段

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@Bean
定义在自动关联其他字段(即
@Bean
类型为EntityManagerFactory)之后被调用,而在自动关联其他字段(即Bean类型为SessionFactory)之前被调用。我只是想知道这背后的原因,因为我自己找不到答案。我认为在自动连接其他字段之前不会调用它(@bean),因为这将违反java JVM。同样,JVM在初始化所有数据成员字段之前不允许任何方法调用。根据您的Hibernate版本,
EntityManagerFactory
SessionFactory
@M.Deinum是的,就是这样。我的第二个代码片段是在
entityManager.unwrap(SessionFactory.class)中抛出NPE