【问题标题】:Runtime error while running a Spring Session + Spring Boot application运行 Spring Session + Spring Boot 应用程序时出现运行时错误
【发布时间】:2015-04-18 17:40:54
【问题描述】:

我正在尝试运行此Spring Session example available here,但我使用的是 Maven 而不是 Gradle。在 a) 运行 WAR 文件和 b) 在 WildFly8.2 服务器中部署 WAR 时,我都收到以下错误

快速搜索表明问题可能是 pom.xml 中引用的各种依赖项之间的版本冲突。

此错误的根本原因和解决方案是什么?

运行或部署 WAR 时出错

java.lang.reflect.InvocationTargetException
    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:483)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.boot.SpringApplicationRunListener : org.springframework.boot.context.event.EventPublishingRunListener
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:381)
    at org.springframework.boot.SpringApplication.getRunListeners(SpringApplication.java:352)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:274)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
    at hello.Application.main(Application.java:25)
    ... 6 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:377)
    ... 11 more
Caused by: java.lang.NoSuchFieldError: INSTANCE
    at org.springframework.boot.SpringApplication.asUnmodifiableOrderedSet(SpringApplication.java:1031)
    at org.springframework.boot.SpringApplication.getListeners(SpringApplication.java:930)
    at org.springframework.boot.context.event.EventPublishingRunListener.<init>(EventPublishingRunListener.java:47)
    ... 16 more

这是我的完整 POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>
    <!-- ... -->
    <packaging>war</packaging>
    <!-- ... -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.10.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
<!--           <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>   -->          
        </dependency>

          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency> 

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


    <dependency>
      <groupId>org.springframework.session</groupId>
      <artifactId>spring-session-data-redis</artifactId>
      <version>1.0.0.RELEASE</version>
      <type>pom</type>
    </dependency>   

     <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.1.3.RELEASE</version>
    </dependency>

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>3.2.5.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    <version>3.2.5.RELEASE</version>
</dependency>  

<dependency>
    <groupId>com.orange.redis-embedded</groupId>
    <artifactId>embedded-redis</artifactId>
    <version>0.5</version>
</dependency>


       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency> 

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>



    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

  • 那个异常意味着你有混合的依赖版本。不要为 Spring 将引入的依赖项指定您自己的版本,如果您需要不包含的其他 Spring 组件,请使用 Spring IO。
  • @chrylis 你是说对于所有 Spring 组依赖项,我最好删除版本?
  • @chrylis 我已经删除了除“spring-session-data-redis”之外的所有 Spring 组依赖项的版本号。如果我没有为此提及版本,我会收到 maven 构建错误,说明我应该提及此版本。但是,如果我只为此提供一个版本并生成 WAR,我仍然会不断收到上述原始错误。

标签: spring maven spring-boot spring-mvc-test spring-session


【解决方案1】:

我看到几个地方需要清理:

  • 您的 pom.xml 中有冲突的 Spring 版本。具体来说,spring-web-4.1.3 被明确列出,Spring Boot 1.1.10 引入了 Spring 4.0.8。从 pom 中删除尚未由 Spring Boot 管理的每个版本(Spring Session 和 Embedded-redis 除外)并让 spring-boot-starter-parent 管理它
  • 一起移除 spring-web 依赖(它已经包含在 spring-boot-starter-web 中)
  • 用 spring-boot-starter-security 替换安全依赖项

您可以在下面找到摘要:

<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>
    <!-- ... -->
    <packaging>war</packaging>
    <!-- ... -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.10.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework.session</groupId>
          <artifactId>spring-session-data-redis</artifactId>
          <version>1.0.0.RELEASE</version>
          <type>pom</type>
        </dependency>

        <dependency>
            <groupId>com.orange.redis-embedded</groupId>
            <artifactId>embedded-redis</artifactId>
            <version>0.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

【讨论】:

  • 这里有一点需要注意 - 您正在使用 com.orange.redis-embedded,它是原始嵌入式 redis 项目的一个分支:github.com/kstyrc/embedded-redis
猜你喜欢
  • 1970-01-01
  • 2017-02-18
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多