2016-10-27 11 views
2

EntityManagerFactory Bean을로드 할 때 다음 예외가 발생합니다.EntityManagerFactory Bean을 작성할 때 IllegalStateException이 발생 함

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

누군가이 오류가 실제로 무엇을 의미하는지 설명 할 수 있습니까? 중복 된 이름을 가진 빈이 아니라는 것을 알기 때문에, 나는 그것의 말씨로 혼란스러워합니다.

답변

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; 
} 
+0

이것은 문제를 해결 한 것으로 보입니다. 이것이 정확히 무엇을했는지 설명해 주시겠습니까? – user856354

+0

'configureEntityManagerFactory'에서'LocalContainerEntityManagerFactoryBean'을 반환 할 때 Spring 컨테이너는'FactoryBean'을 인식하고'@Bean'으로 주석 처리 된'getObject()'메소드를 호출하여'EntityManagerFactory' 인스턴스의 인스턴스를 생성합니다. 그러나이'configureEntityManagerFactory(). getObject()'를 다시'annotationDrivenTransactionManager' 메쏘드에서 호출 할 때, 당신은이 인스턴스를 생성하는 두 개의 인스턴스를 생성하는 인스턴스 인'EntityManagerFactory'를 다시 만듭니다. – shazin

+1

참고 : Spring 작업을 수행하는 방법은 다음과 같습니다. 1)'FactoryBean '를 반환하고'.afterPropertiesSet()'과'.getObject()'를 제거하고'entityManagerFactoryBean'을 반환하도록'configureEntityManagerFactory' 메소드를 변경하십시오. 2)'EntityManagerFactory entityManagerFactory' 인자를'annotationDrivenTransactionManager' 메쏘드에 추가하고'configureEntityManagerFactory()'를 호출하는 대신에 그 인자를 사용하십시오. Spring이 이미 알고 있기 때문에 @DependsOn ("entityManagerFactoryBean") 주석을 제거 할 수도 있습니다. – Roman