【问题标题】:Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationContextInitializer线程“主”java.lang.IllegalArgumentException 中的异常:无法实例化接口 org.springframework.context.ApplicationContextInitializer
【发布时间】:2016-10-09 13:36:50
【问题描述】:

我面临以下错误:

线程“主”java.lang.IllegalArgumentException 中的异常:不能 实例化接口 org.springframework.context.ApplicationContextInitializer : org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer 在 org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:414) 在 org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:394) 在 org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:385) 在 org.springframework.boot.SpringApplication.initialize(SpringApplication.java:261) 在 org.springframework.boot.SpringApplication.(SpringApplication.java:237) 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)

我的主要方法是

package proj1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplication.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(WebApplication.class, args);
    }
}

我的 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.webapp</groupId>
  <artifactId>proj1</artifactId>
  <packaging>war</packaging>
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>
  <version>0.0.1-SNAPSHOT</version>
  <name>proj1 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
   <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
     <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>1.4.0.RELEASE</version>
</dependency> 
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
  </dependencies>
  <build>
    <finalName>proj1</finalName>
  </build>
</project>

【问题讨论】:

  • 我不相信这是错误的完整堆栈跟踪。
  • 对于初学者,请停止混合框架版本(1.3.5 和 1.4.0)。混合版本的框架(不管框架)是必然会发生的麻烦。接下来,您使用的是 Spring 4.0.0 依赖项,而 4.2 是预期的(删除已包含的 spring-core 依赖项)。
  • 如果我将版本更改为任何版本,即 1.3.5 或 1.4.0,我尝试了这个 @M.Deinum 有导入 org.springframework.boot.SpringApplication 无法解决错误,这就是原因我保持版本不同。请参阅帖子link
  • 再一次,永远不要混合一个框架的版本,因为那是等待发生的麻烦,更不用说你将得到的 Spring 框架版本的混合。解决真正的问题,而不是混合你一开始就不应该混合的东西。

标签: java spring-boot


【解决方案1】:

我最近遇到了这个问题。我正在通过一个 bat 文件运行一个可执行的 jar。我的可执行 jar 已经拥有 pom.xml 中提到的版本所需的所有 Spring Boot jar。但是我错误地在bat文件中定义了一个spring-boot库(其他版本)的类路径。当我删除该类路径条目时,这个特定问题就消失了,因为它只使用了 pom 文件中提到的 spring boot jar。我认为这个问题的出现是因为 spring jar 的版本冲突。如果我错了,请纠正我。谢谢。

【讨论】:

    【解决方案2】:

    你的&lt;dependencies&gt; 应该是这样的

    <?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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.webapp</groupId>
        <artifactId>proj1</artifactId>
        <packaging>war</packaging>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.3.5.RELEASE</version>
        </parent>
        <version>0.0.1-SNAPSHOT</version>
        <name>proj1 Maven Webapp</name>
        <url>http://maven.apache.org</url>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <scope>provided</scope>
            </dependency>
            <!-- Test Dependencies -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <finalName>proj1</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    

    而不是像您那样混合使用 Spring Boot(1.3 和 1.4)版本和 Spring 版本(4.0、4.2 和 4.3)。您还包含了已经包含(或破坏)的依赖项。

    另一件事是你应该有 spring-boot-maven 插件,它在你的 pom.xml 中也没有。

    【讨论】:

    • 我试过这个 pom 现在它给出了错误 The import org.springframework.boot.autoconfigure.SpringBootApplication cannot be resolve and SpringBootApplication cannot be resolve to a type
    • 修复您的 Eclipse 设置(清理并重新导入)...与其依赖 Eclipse,不如尝试从命令行构建。如果这样可行,那么你的日食就很糟糕了。
    • 我正在使用 STS,我已经清理并重新导入了很多次,但即使我创建新项目也存在同样的问题,但同样的问题仍然存在。
    • STS == Eclipse... 如前所述,尝试命令行,如果可以的话,你的 IDE 就坏了。
    • mvn clean package 并在此之前尝试mvn dependency:purge-local-repository,它将清理您的本地存储库并强制重新下载所有依赖项。
    【解决方案3】:

    spring-boot-starter-parent 的版本从 2.0.0 更改为 2.1.3 对我有用。

    【讨论】:

      【解决方案4】:
      public class MyPolicy extends Policy {
          @Override
          public PermissionCollection getPermissions(CodeSource codesource) {
              Permissions p = new Permissions();
              /**
                * The AllPermission is a permission that implies all other permissions.
                * <p>
                * <b>Note:</b> Granting AllPermission should be done with extreme care,
                * as it implies all other permissions. Thus, it grants code the ability
                * to run with security
                * disabled.  Extreme caution should be taken before granting such
                * a permission to code.  This permission should be used only during testing,
                * or in extremely rare cases where an application or applet is
                * completely trusted and adding the necessary permissions to the policy
                * is prohibitively cumbersome.
                *
                * @see java.security.Permission
                * @see java.security.AccessController
                * @see java.security.Permissions
                * @see java.security.PermissionCollection
                * @see java.lang.SecurityManager
                *
                *
                * @author Roland Schemers
                * @since 1.2
                *
                * @serial exclude
      
      
               public final class AllPermission*/
              //p.add(new AllPermission());
              //p.add(new CryptoPermission());
              p.add(new AudioPermission());
              p.add(new AuthPermission());
              p.add(new AWTPermission());
              p.add(new BasicPermission());
              p.add(new DelegationPermission());
              p.add(new FilePermission());
              p.add(new JAXBPermission());
              p.add(new LinkPermission());
              p.add(new LoggingPermission());
              p.add(new ManagementPermission());
              p.add(new MBeanPermission());
              p.add(new MBeanServerPermission());
              p.add(new MBeanTrustPermission());
              p.add(new NetPermission());
              p.add(new Permission());
              p.add(new PrivateCredentialPermission());
              p.add(new PropertyPermission());
              p.add(new ReflectPermission());
              p.add(new RuntimePermission());
              p.add(new SecurityPermission());
              p.add(new SerializablePermission());
              p.add(new ServicePermission());
              p.add(new SocketPermission());
              p.add(new SQLPermission());
              p.add(new SSLPermission());
              p.add(new SubjectDelegationPermission());
              p.add(new UnresolvedPermission());
              p.add(new URLPermission());
              p.add(new WebServicePermission());
              return p;
          }
      }
      
      public  void main(String[] args) {
          Policy.setPolicy(new MyPolicy());
      
          SpringApplication.run(Application.class, args);
      }
      

      【讨论】:

        猜你喜欢
        • 2019-01-23
        • 2018-11-27
        • 1970-01-01
        • 2020-05-09
        • 2021-12-21
        • 1970-01-01
        • 1970-01-01
        • 2020-07-18
        • 1970-01-01
        相关资源
        最近更新 更多