java 上下文初始化失败——BeanCreationNotAllowedException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7493530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:12:54  来源:igfitidea点击:

Context Initialization Failure -- BeanCreationNotAllowedException

javaspringdependency-injectioncxf

提问by kin1

The Spring Context of my application is failing to initialize. Can anyone help me understand why it is failing and how to fix it?

我的应用程序的 Spring Context 无法初始化。谁能帮助我理解它为什么失败以及如何解决它?

Below are the warning & error messages I'm getting:

以下是我收到的警告和错误消息:

[WARN] Invocation of destroy method 'shutdown' failed on bean with name 'cxf'

org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'entityManagerFactory': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)

org.springframework.web.context.ContextLoader [ERROR] Context initialization failed

[警告] 对名为“cxf”的 bean 调用销毁方法“关闭”失败

org.springframework.beans.factory.BeanCreationNotAllowedException:创建名为“entityManagerFactory”的 bean 时出错:在销毁该工厂的单例时不允许创建单例 bean(不要在销毁方法实现中从 BeanFactory 请求 bean!)

org.springframework.web.context.ContextLoader [ERROR] 上下文初始化失败

<bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl" destroy-method="shutdown"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
       <property name="persistenceUnitName" value="${persistence.unit}"/>
       <property name="dataSource" ref="pooledDs"/>
       <property name="jpaVendorAdapter">
           <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
               <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
               <property name="showSql" value="false"/>
               <property name="generateDdl" value="false"/>
           </bean>
       </property>    
</bean> 

回答by atrain

The CXF website doesn't include the destroy-methodcall in its example configurations, so it seems like this is a misconfiguration. See this page for details: http://cxf.apache.org/docs/interceptors.html.

CXF 网站destroy-method在其示例配置中不包含该调用,因此这似乎是一个错误配置。有关详细信息,请参阅此页面:http: //cxf.apache.org/docs/interceptors.html

I also found a bug tracker for this issue: https://issues.apache.org/jira/browse/CXF-2164. It appears that the destroy method was not implicitly being called in earlier versions of CXF, but that has been fixed in v2.2.11.

我还找到了针对此问题的错误跟踪器:https: //issues.apache.org/jira/browse/CXF-2164。在早期版本的 CXF 中似乎没有隐式调用 destroy 方法,但在 v2.2.11 中已修复。

So, my suggestion would be to get up to at least that version and just have <bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl" />in your config.

所以,我的建议是至少升级到那个版本,然后<bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl" />在你的配置中加入。

回答by Betlista

In my case I had same problem with version 2.5.0, but that was my fault.

就我而言,我在 2.5.0 版中遇到了同样的问题,但那是我的错。

I had wrong bean in context.

我在上下文中有错误的 bean。

In detail: I had Spring MVC Controller (named OrderController) annotated with @Controllerwithout defined name (annotation driven). On the other CXF requires xml configuration AFAIK, so I named bean using java configuration (using @Bean) as orderControllerand somehow when cxf was initialized Spring used this wrong MVC controller and it failed on error listed above.

详细说明:我对 Spring MVC 控制器(名为 OrderController)进行了注释,@Controller但未定义名称(注释驱动)。在另一个 CXF 需要 xml 配置 AFAIK,所以我使用 java 配置(使用@Bean)命名 bean ,orderController并且在 cxf 初始化时以某种方式 Spring 使用了这个错误的 MVC 控制器,并且它因上面列出的错误而失败。

org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'cxf': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)

org.springframework.beans.factory.BeanCreationNotAllowedException:创建名为“cxf”的 bean 时出错:当该工厂的单例处于销毁状态时,不允许创建单例 bean(不要在销毁方法实现中从 BeanFactory 请求 bean!)