Java BeanFactory 未初始化或已关闭 - 之前调用“刷新”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20114955/
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
BeanFactory not initialized or already closed - call 'refresh' before
提问by PayBash
here is my error:
这是我的错误:
I am getting this error , can any one help me on this.
我收到此错误,任何人都可以帮助我。
> SEVERE: Exception sending context initialized event to listener
> instance of class
> org.springframework.web.context.ContextLoaderListener
> java.lang.NoSuchMethodError:
> org.springframework.beans.factory.xml.XmlBeanDefinitionReader.setEnvironment(Lorg/springframework/core/env/Environment;)V
> at
> org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:87)
> at
> org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
> at
> org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)
> at
> org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)
> at
> org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
> at
> org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
> at
> org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
> at
> org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)
> at
> org.apache.catalina.core.StandardContext.call(StandardContext.java:5226)
> at
> org.apache.catalina.core.StandardContext.call(StandardContext.java:5221)
> at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) at
> java.util.concurrent.FutureTask.run(Unknown Source) at
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
> at java.lang.Thread.run(Unknown Source) Nov 21, 2013 12:54:38 PM
> org.apache.catalina.core.StandardContext startInternal SEVERE: Error
> listenerStart Nov 21, 2013 12:54:38 PM
> org.apache.catalina.core.StandardContext startInternal SEVERE: Context
> [/FLPalette] startup failed due to previous errors Nov 21, 2013
> 12:54:38 PM org.apache.catalina.core.ApplicationContext log INFO:
> Closing Spring root WebApplicationContext Nov 21, 2013 12:54:38 PM
> org.apache.catalina.core.StandardContext listenerStop SEVERE:
> Exception sending context destroyed event to listener instance of
> class org.springframework.web.context.ContextLoaderListener
> java.lang.IllegalStateException: BeanFactory not initialized or
> already closed - call 'refresh' before accessing beans via the
> ApplicationContext at
> org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172)
> at
> org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1066)
> at
> org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1040)
> at
> org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:988)
> at
> org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:556)
> at
> org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:142)
> at
> org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4763)
> at
> org.apache.catalina.core.StandardContext.run(StandardContext.java:5472)
> at java.lang.Thread.run(Unknown Source) at
> org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5481)
> at
> org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:225)
> at
> org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
> at
> org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1033)
> at
> org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:774)
> at
> org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
> at
> org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1033)
> at
> org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:291)
> at
> org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
> at
> org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
> at
> org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
> at
> org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:727)
> at
> org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
> at org.apache.catalina.startup.Catalina.start(Catalina.java:620) at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
> sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
> java.lang.reflect.Method.invoke(Unknown Source) at
> org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:303) at
> org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:431) Nov 21,
> 2013 12:54:38 PM org.apache.coyote.AbstractProtocol start INFO:
> Starting ProtocolHandler ["http-bio-8080"] Nov 21, 2013 12:54:38 PM
> org.apache.coyote.AbstractProtocol start INFO: Starting
> ProtocolHandler ["ajp-bio-8009"] Nov 21, 2013 12:54:38 PM
> org.apache.catalina.startup.Catalina start
pom.xml :
pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.paypal.palette</groupId>
<artifactId>FLPalette</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>FLPalette</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.1.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.1.1.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.1.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.1.1.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.1.1.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.1.1.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.1.1.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.1.1.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
<version>3.1.1.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.50</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<properties>
<org.springframework.version>3.0.2.RELEASE</org.springframework.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
回答by Will Keeling
The issue here is that the version of spring-web
that you're using (3.1.1-RELEASE) is not compatible with the version of spring-beans
that you're using (3.0.2-RELEASE). At the top of the stack you can see the NoSuchMethodError
which in turn triggers the BeanFactory not initialized...
exception.
这里的问题是spring-web
您使用的版本(3.1.1-RELEASE) 与spring-beans
您使用的版本(3.0.2-RELEASE)不兼容。在堆栈的顶部,您可以看到NoSuchMethodError
触发BeanFactory not initialized...
异常的 。
The NoSuchMethodError
is caused because the method invocation XmlWebApplicationContext.loadBeanDefinitions()
in spring-web
is trying to call XmlBeanDefinitionReader.setEnvironment()
in spring-beans
which doesn't exist in 3.0.2-RELEASE. It does however exist in 3.1.1-RELEASE - as setEnvironment
is inherited from the parent AbstractBeanDefinitionReader
.
的NoSuchMethodError
,因为方法调用导致XmlWebApplicationContext.loadBeanDefinitions()
在spring-web
试图调用XmlBeanDefinitionReader.setEnvironment()
在spring-beans
不3.0.2-RELEASE存在。然而,它确实存在于 3.1.1-RELEASE 中——因为它setEnvironment
是从 parent 继承的AbstractBeanDefinitionReader
。
To resolve, you'd probably be best to upgrade the spring-beans
jar to 3.1.1-RELEASE. The version for this jar appears to be parameterized in your pom.xml and is controlled by the property org.springframework.version
further down in the file.
要解决,您可能最好将spring-beans
jar升级到 3.1.1-RELEASE。此 jar 的版本似乎在您的 pom.xml 中参数化,并由org.springframework.version
文件中更下方的属性控制。
回答by NIrav Modi
This exception come due to you are providing listener ContextLoaderListener
此异常是由于您提供侦听器 ContextLoaderListener
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
but you are not providing context-param
for your spring configuration file. like applicationContext.xml
You must provide below snippet for your configuration
但你没有提供context-param
你的 spring 配置文件。就像applicationContext.xml
您必须为您的配置提供以下代码段
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>applicationContext.xml</param-value>
</context-param>
If You are providing the java based spring configuration , means you are not using xml file for spring configuration at that time you must provide below code:
如果您提供基于 java 的 spring 配置,则意味着您当时没有使用 xml 文件进行 spring 配置,您必须提供以下代码:
<!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified @Configuration classes. Fully-qualified packages may also
be specified for component-scanning -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.nirav.modi.config.SpringAppConfig</param-value>
</context-param>
回答by Fuzzy Analysis
I had this issue until I removed the project in question from the server's deployments (in JBoss Dev Studio, right-click the server and "Remove" the project in the Servers view), then did the following:
我遇到了这个问题,直到我从服务器的部署中删除了有问题的项目(在 JBoss Dev Studio 中,右键单击服务器并在“服务器”视图中“删除”该项目),然后执行以下操作:
- Restarted the JBoss EAP 6.1 server without any projects deployed.
- Once the server had started, I then added the project in question to the server.
- 在没有部署任何项目的情况下重新启动 JBoss EAP 6.1 服务器。
- 服务器启动后,我将有问题的项目添加到服务器。
After this, just restart the server (in debug or run mode) by selecting the server, NOT the project itself.
在此之后,只需通过选择服务器重新启动服务器(在调试或运行模式下),而不是项目本身。
This seemed to flush any previous settings/states/memory/whatever that was causing the issue, and I no longer got the error.
这似乎刷新了任何以前的设置/状态/内存/导致问题的任何内容,并且我不再收到错误消息。
回答by Alessandro C
In my case, the error "BeanFactory not initialized or already closed - call 'refresh' before" was a consequence of a previous error that I didn't noticed in the server startup. I think that it is not always the real cause of the problem.
就我而言,错误“BeanFactory 未初始化或已经关闭 - 之前调用‘刷新’”是我在服务器启动时没有注意到的先前错误的结果。我认为这并不总是问题的真正原因。
回答by jprism
I came across this issue twice once in upgrading to 3.2.18 from 3.2.1 and 4.3.5 from 3.2.8. In both cases, this error is because of different version of spring modules
我在从 3.2.1 升级到 3.2.18 和从 3.2.8 升级到 4.3.5 时遇到过两次这个问题。在这两种情况下,这个错误都是因为不同版本的 spring 模块
回答by Ateeque Shaikh
In my case, this error was due to the Network connection error that i was noticed in log.
就我而言,此错误是由于我在日志中注意到的网络连接错误造成的。
回答by hi.nitish
In my case, the error was valid and it was due to using try with resource
在我的情况下,错误是有效的,这是由于使用 try with resource
try (ConfigurableApplicationContext cxt = new ClassPathXmlApplicationContext(
"classpath:application-main-config.xml")) {..
}
It auto closes the stream which should not happen if I want to reuse this context in other beans.
如果我想在其他 bean 中重用这个上下文,它会自动关闭不应该发生的流。
回答by javatogo
I had the same error and I had not made any changes to the application config or the web.xml. Multiple tries to revert back some minor changes to code was not clearing the exceptions. Finally it worked after restarting STS.
我有同样的错误,我没有对应用程序配置或 web.xml 进行任何更改。多次尝试恢复对代码的一些小的更改并没有清除异常。最后它在重新启动 STS 后工作。
回答by Qian Zhang
In the spring framework inside the springframework initialization Repository or controller annotation, the same class name can only exist a default instance, you can set the value name
在springframework里面的springframework初始化Repository或者controller注解中,相同的类名只能存在一个默认实例,可以设置值名
回答by Abderrazzak Nejeoui
This problem can be caused also by jvm version used to compile the project and the jvm supported by the servlet container. Try to Fix the project build path. For example if you deploy on tomcat 9, use jvm 1.8.0 or lower.
这个问题也可能是编译项目所用的jvm版本和servlet容器支持的jvm引起的。尝试修复项目构建路径。例如,如果您在 tomcat 9 上部署,请使用 jvm 1.8.0 或更低版本。