Spring嵌套的应用程序上下文“没有bean命名'entityManagerFactory'被定义”

时间:2016-03-30 23:01:40

标签: spring spring-boot spring-data

我需要在Spring启动应用程序中的一个独立的应用程序上下文中导出一堆JPARepositories然后我将我需要的服务从隔离的应用程序上下文中提取并发布到全局上下文中。

配置类如下:

@Configuration
@Import({PersistenceAutoConfiguration.class, DaoServicesConfiguration.class})
public class TestApiAutoconfiguration implements InitializingBean {
    private AnnotationConfigApplicationContext myApplicationContext;

    @Autowired
    LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory;

    @Autowired
    ConfigService configService;

    @Autowired
    FeatureService featureService;

    @Override
    public void afterPropertiesSet() throws Exception {
        myApplicationContext = new AnnotationConfigApplicationContext();

        myApplicationContext.getBeanFactory().registerSingleton("entityManagerFactory", primaryEntityManagerFactory.getObject());
        myApplicationContext.getBeanFactory().registerSingleton("configService", configService);
        myApplicationContext.getBeanFactory().registerSingleton("featureService", featureService);

        myApplicationContext.register(TestApiConfiguration.class);
        myApplicationContext.refresh();
    }

    @Bean(name = "/TestApi.json")
    JsonServiceExporter jsonServiceExporter() {
        TestApi testApi = myApplicationContext.getBean(TestApi.class);
        CustomObjectMapper customObjectMapper =    
            myApplicationContext.getBean(CustomObjectMapper.class);

        JsonServiceExporter jsonServiceExporter = new JsonServiceExporter();
        jsonServiceExporter.setService(testApi);
        jsonServiceExporter.setServiceInterface(TestApi.class);
        jsonServiceExporter.setObjectMapper(customObjectMapper);

        return jsonServiceExporter;
    }
}

我遇到以下异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'beanNameHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.payliquid.autoconfigure.TestApiAutoconfiguration': Invocation of init method failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

我明确地发布了entityManagerFactory,为什么我得到这个异常?

其次,为什么异常跟踪提到WebMvcAutoConfiguration类?

1 个答案:

答案 0 :(得分:0)

以下解决方案是一种解决方法。如果您知道更好的实现方法,请发表回答

根据有关entityManagerFactory bean的生命周期问题的一些挖掘在弹簧数据的编程配置中非常常见。

我在进行标准弹簧启动时也遇到过这个问题,需要应用以下模式才能获得正确的生命周期排序。

@Configuration
@EnableJpaRepositories(...)
class AConfigurationClass {
   @Bean 
   EntityManagerFactory entityManagerFactory() {
       //..
   }
}

为了符合这种模式,我决定手动将字段注入静态变量 - 即。,

@Configuration
@EnableJpaRepositories(basePackageClasses = {AcquirerDAO.class})
public class TestApiConfiguration {
    private static EntityManagerFactory entityManagerFactory;
    private static DataSource datasource;

    public static void setEntityManagerFactory(EntityManagerFactory emf) {
        entityManagerFactory = emf;
    }

    public static void setDatasource(DataSource ds) {
        datasource = ds;
    }

    @Bean
    EntityManagerFactory entityManagerFactory() {
        return entityManagerFactory;
    }

    @Bean
    DataSource dataSource() {
        return datasource;
    }
 //...
}