Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.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
Java 创建EntityManagerFactory Bean时出现非法状态异常_Java_Spring Transactions_Spring Bean_Spring Orm - Fatal编程技术网

Java 创建EntityManagerFactory Bean时出现非法状态异常

Java 创建EntityManagerFactory Bean时出现非法状态异常,java,spring-transactions,spring-bean,spring-orm,Java,Spring Transactions,Spring Bean,Spring Orm,在尝试加载EntityManagerFactorybean时,我遇到以下异常 SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bea

在尝试加载EntityManagerFactorybean时,我遇到以下异常

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

有人能向我解释这个错误的真正含义吗?鉴于它不是一个名称重复的bean,我对它的措辞感到困惑。

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

@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;
}

这似乎解决了问题。你能解释一下这到底是怎么回事吗?当你从
configureEntityManagerFactory
返回
LocalContainerEntityManagerFactoryBean
时,Spring容器会识别出它有一个
FactoryBean
并调用它的
getObject()
方法创建用
@Bean注释的
EntityManagerFactory
实例的实例。但是当您调用这个
configureEntityManagerFactory().getObject()时
再次从
annotationDrivenTransactionManager
方法创建一个实例
EntityManagerFactory
,该实例会导致两个实例,从而导致问题。注意:Spring的方法是:1)更改
configureEntityManagerFactory
方法以返回
FactoryBean
,删除
.afterPropertieSet()
.getObject()
并返回
entityManagerFactoryBean
。2) 将
EntityManagerFactory EntityManagerFactory
参数添加到
annotationDrivenTransactionManager
方法中,并使用该参数而不是调用
configureEntityManagerFactory()
。您还可以删除
@DependsOn(“entityManagerFactoryBean”)
注释,因为spring已经知道了这一点。
@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;
}