cannot access org.springframework.core.env.Environ

2020-08-12 06:05发布

问题:

I'm trying to get a spring bean in a web application using it:

WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
AClass aClass = (aClass) wac.getBean("aClass");

And, when I run compile/test/package with maven, an error occurs:

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! :/

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

Any idea is welcome!

回答1:

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:

<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.

I hope that it helps more people.



回答2:

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.

The solution im my case was delete the .m2 folder, so maven downloaded all the jars again. Maybe it was some currupted file.

I hope it helps some one!



回答3:

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.4 moving to 2.2.5 (or latest releases ) will download all the needed dependencies

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>


回答4:

Me too face this issue. I used Spring 4.1.6 with maven 3 along with 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.



回答5:

Add this dependency to the pom.xml .You can fixed it.

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>1.4.2.RELEASE</version>        
</dependency>