创建EntityManagerFactory Bean

时间:2016-10-27 14:25:26

标签: java spring-transactions spring-bean spring-orm

尝试加载EntityManagerFactory Bean时,我收到以下异常。

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactoryBean' defined in com.global.us.app.config.PersistenceConfig: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'annotationDrivenTransactionManager' defined in com.global.us.app.config.PersistenceConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.transaction.PlatformTransactionManager]: Factory method 'annotationDrivenTransactionManager' threw exception; nested exception is java.lang.IllegalStateException: @Bean method PersistenceConfig.configureEntityManagerFactory called as a bean reference for type [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] but overridden by non-compatible bean instance of type [com.sun.proxy.$Proxy30]. Overriding bean of same name declared in: com.global.us.app.config.PersistenceConfig
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.doCreateBean(AbstractAutowirapppableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.createBean(AbstractAutowirapppableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1054)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:829)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4758)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5184)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'annotationDrivenTransactionManager' defined in com.global.us.app.config.PersistenceConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.transaction.PlatformTransactionManager]: Factory method 'annotationDrivenTransactionManager' threw exception; nested exception is java.lang.IllegalStateException: @Bean method PersistenceConfig.configureEntityManagerFactory called as a bean reference for type [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] but overridden by non-compatible bean instance of type [com.sun.proxy.$Proxy30]. Overriding bean of same name declared in: com.global.us.app.config.PersistenceConfig
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.populateBean(AbstractAutowirapppableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.doCreateBean(AbstractAutowirapppableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.createBean(AbstractAutowirapppableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:368)
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowirapppableBeanFactory.java:1123)
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.createBeanInstance(AbstractAutowirapppableBeanFactory.java:1018)
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.doCreateBean(AbstractAutowirapppableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.createBean(AbstractAutowirapppableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans(BeanFactoryAdvisorRetrievalHelper.java:92)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors(AbstractAdvisorAutoProxyCreator.java:101)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:87)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:69)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:346)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:298)
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowirapppableBeanFactory.java:422)
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.initializeBean(AbstractAutowirapppableBeanFactory.java:1583)
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.doCreateBean(AbstractAutowirapppableBeanFactory.java:545)
... 20 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'annotationDrivenTransactionManager' defined in com.global.us.app.config.PersistenceConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.transaction.PlatformTransactionManager]: Factory method 'annotationDrivenTransactionManager' threw exception; nested exception is java.lang.IllegalStateException: @Bean method PersistenceConfig.configureEntityManagerFactory called as a bean reference for type [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] but overridden by non-compatible bean instance of type [com.sun.proxy.$Proxy30]. Overriding bean of same name declared in: com.global.us.app.config.PersistenceConfig
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowirapppableBeanFactory.java:1123)
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.createBeanInstance(AbstractAutowirapppableBeanFactory.java:1018)
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.doCreateBean(AbstractAutowirapppableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowirapppableBeanFactory.createBean(AbstractAutowirapppableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:381)
at com.global.us.app.config.PersistenceConfig$$EnhancerBySpringCGLIB$$9735d908.annotationDrivenTransactionManager(<generated>)
at org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration.setConfigurers(AbstractTransactionManagementConfiguration.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:654)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 45 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.transaction.PlatformTransactionManager]: Factory method 'annotationDrivenTransactionManager' threw exception; nested exception is java.lang.IllegalStateException: @Bean method PersistenceConfig.configureEntityManagerFactory called as a bean reference for type [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] but overridden by non-compatible bean instance of type [com.sun.proxy.$Proxy30]. Overriding bean of same name declared in: com.global.us.app.config.PersistenceConfig
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 63 more
Caused by: java.lang.IllegalStateException: @Bean method PersistenceConfig.configureEntityManagerFactory called as a bean reference for type [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] but overridden by non-compatible bean instance of type [com.sun.proxy.$Proxy30]. Overriding bean of same name declared in: com.global.us.app.config.PersistenceConfig
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:394)
at com.global.us.app.config.PersistenceConfig$$EnhancerBySpringCGLIB$$9735d908.configureEntityManagerFactory(<generated>)
at com.global.us.app.config.PersistenceConfig.annotationDrivenTransactionManager(PersistenceConfig.java:72)
at com.global.us.app.config.PersistenceConfig$$EnhancerBySpringCGLIB$$9735d908.CGLIB$annotationDrivenTransactionManager$3(<generated>)
at com.global.us.app.config.PersistenceConfig$$EnhancerBySpringCGLIB$$9735d908$$FastClassBySpringCGLIB$$dd07c011.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
at com.global.us.app.config.PersistenceConfig$$EnhancerBySpringCGLIB$$9735d908.annotationDrivenTransactionManager(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 64 more

我认为这是一个有两个同名豆子的情况。但是,当我在代码库中搜索“entityManagerFactoryBean”时,没有其他引用。

@Bean(name="dataSource")
public DataSource getDataSource() {
    if ( this.dataSource == null) {
        this.dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(env.getProperty(DRIVER_CLASS_NAME_PROPERTY));
        dataSource.setUrl(env.getProperty(DB_URL_PROPERTY));
        dataSource.setUsername(env.getProperty(USERNAME_PROPERTY));
        dataSource.setPassword(env.getProperty(PASSWORD_PROPERTY));
    }
    return this.dataSource;
}

@Bean(name="entityManagerFactoryBean")
public LocalContainerEntityManagerFactoryBean configureEntityManagerFactory() {

    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setDataSource(getDataSource());
    entityManagerFactoryBean.setPackagesToScan("com.global.us.app.model.db");
    entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());

    Properties jpaProperties = new Properties();
    jpaProperties.put(org.hibernate.cfg.Environment.DIALECT, env.getProperty(org.hibernate.cfg.Environment.DIALECT));
    jpaProperties.put(org.hibernate.cfg.Environment.SHOW_SQL, env.getProperty(org.hibernate.cfg.Environment.SHOW_SQL));
    jpaProperties.put(org.hibernate.cfg.Environment.HBM2DDL_IMPORT_FILES_SQL_EXTRACTOR, env.getProperty(org.hibernate.cfg.Environment.HBM2DDL_IMPORT_FILES_SQL_EXTRACTOR));
    entityManagerFactoryBean.setJpaProperties(jpaProperties);

    return entityManagerFactoryBean;
} 

@Override
@Bean()
@DependsOn("entityManagerFactoryBean")
public PlatformTransactionManager annotationDrivenTransactionManager() {
    JpaTransactionManager jpa =  new JpaTransactionManager();
    jpa.setEntityManagerFactory(configureEntityManagerFactory().getObject());
    return jpa;
}

有人能够向我解释这个错误究竟意味着什么吗?看到它不是一个名字重复的bean,我对它的措辞感到困惑。

1 个答案:

答案 0 :(得分:4)

请尝试更改您的方法以创建EntityManagerFactoryPlatformTransactionManager

@Bean(name="entityManagerFactoryBean")
public EntityManagerFactory configureEntityManagerFactory() {

    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setDataSource(getDataSource());
    entityManagerFactoryBean.setPackagesToScan("com.global.us.app.model.db");
    entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());

    Properties jpaProperties = new Properties();
    jpaProperties.put(org.hibernate.cfg.Environment.DIALECT, env.getProperty(org.hibernate.cfg.Environment.DIALECT));
    jpaProperties.put(org.hibernate.cfg.Environment.SHOW_SQL, env.getProperty(org.hibernate.cfg.Environment.SHOW_SQL));
    jpaProperties.put(org.hibernate.cfg.Environment.HBM2DDL_IMPORT_FILES_SQL_EXTRACTOR, env.getProperty(org.hibernate.cfg.Environment.HBM2DDL_IMPORT_FILES_SQL_EXTRACTOR));
    entityManagerFactoryBean.setJpaProperties(jpaProperties);

    entityManagerFactoryBean.afterPropertiesSet();

    return (EntityManagerFactory) entityManagerFactoryBean.getObject();
} 


@Override
@Bean()
@DependsOn("entityManagerFactoryBean")
public PlatformTransactionManager annotationDrivenTransactionManager() {
    JpaTransactionManager jpa =  new JpaTransactionManager();
    jpa.setEntityManagerFactory(configureEntityManagerFactory());
    return jpa;
}