Java 无法访问 org.springframework.core.env.EnvironmentCapable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25488194/
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
cannot access org.springframework.core.env.EnvironmentCapable
提问by Ellison Alves
I'm trying to get a spring bean in a web application using it:
我正在尝试使用它在 Web 应用程序中获取一个 spring bean:
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
AClass aClass = (aClass) wac.getBean("aClass");
And, when I run compile/test/package with maven, an error occurs:
而且,当我使用 maven 运行 compile/test/package 时,会发生错误:
cannot access org.springframework.core.env.EnvironmentCapable
[ERROR] class file for org.springframework.core.env.EnvironmentCapable not found
The most strange is that org.springframework.core.env.EnvironmentCapable exists! :/
最奇怪的是 org.springframework.core.env.EnvironmentCapable 存在!:/
Basic Project Configuration:
基本项目配置:
- Spring 3.1.1.RELEASE (There isn't other spring version in classpath)
- Maven 3
- JSF 2.1
- Servlet API 2.5
- Spring 3.1.1.RELEASE (classpath 中没有其他 spring 版本)
- Maven 3
- JSF 2.1
- 小服务程序 API 2.5
Any idea is welcome!
欢迎任何想法!
回答by Ellison Alves
Finally, I've solved it! :)
终于,我解决了!:)
In the pom.xml file, I had to define the scope of spring's dependencies to compile. (Yes, I know that is the default scope of dependencies but for some reason maven was not capable of the job). See a piece of my pom.xml that made the problem disapear:
在 pom.xml 文件中,我必须定义要编译的 spring 依赖项的范围。(是的,我知道这是依赖项的默认范围,但由于某种原因 maven 无法胜任这项工作)。查看我的 pom.xml 的一部分,它使问题消失了:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
Note that:
注意:
- If you're having this problem, make sure that you're using a spring version 3.1 or higher.
- Remember that ${spring.version}, in my case, is 3.1.1.RELEASE.
- 如果您遇到此问题,请确保您使用的是 Spring 3.1 或更高版本。
- 请记住,在我的情况下,${spring.version} 是 3.1.1.RELEASE。
I hope that it helps more people.
我希望它能帮助更多的人。
回答by Ronnie
Me too face this issue. I used Spring 4.1.6 with maven 3 along with RabbitMQ.
我也面临这个问题。我使用 Spring 4.1.6 和 maven 3 以及 RabbitMQ。
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-amqp</artifactId>
<version>1.1.4.RELEASE</version>
</dependency>
This dependency forced tomcat to die and showing these issue. I havn't gotten why it is making trouble. but finally I explicitly include jar in lib folder and this is how I resolved this issue.
这种依赖性迫使 tomcat 死亡并显示这些问题。我不明白为什么它会惹麻烦。但最后我明确地将 jar 包含在 lib 文件夹中,这就是我解决此问题的方法。
回答by Matrix
Add this dependency to the pom.xml .You can fixed it.
将此依赖项添加到 pom.xml 。您可以修复它。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
回答by Fábio Magagnin
I was facing the exact same issue, maven complains from org.springframework.core.env.EnvironmentCapable
, even with the file there, inside the jar: C:\Users\fabio\.m2\repository\org\springframework\spring-core\4.3.12.RELEASE\spring-core-4.3.12.RELEASE.jar
.
我面临着完全相同的问题,Maven 抱怨org.springframework.core.env.EnvironmentCapable
,即使文件在那里,在 jar 中:C:\Users\fabio\.m2\repository\org\springframework\spring-core\4.3.12.RELEASE\spring-core-4.3.12.RELEASE.jar
.
The solution im my case was delete the .m2
folder, so maven downloaded all the jars again. Maybe it was some currupted file.
我的解决方案是删除该.m2
文件夹,因此 maven 再次下载了所有 jars。也许这是一些损坏的文件。
I hope it helps some one!
我希望它可以帮助某人!
回答by ikbel benabdessamad
As I imported a project from GitHub without checking its spring-boot version had caused me the same problem, thus changing spring-boot version has resolved my problem, I was using version 1.1.4moving to 2.2.5(or latest releases ) will download all the needed dependencies
由于我从 GitHub 导入了一个项目而没有检查其 spring-boot 版本给我带来了同样的问题,因此更改 spring-boot 版本解决了我的问题,我使用的版本1.1.4移至2.2.5(或最新版本)将下载所有需要的依赖项
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>