线程“main”中的异常 java.lang.IllegalArgumentException:无法实例化接口 org.springframework.context.ApplicationContextInitializer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39944244/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:47:30  来源:igfitidea点击:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationContextInitializer

javaspring-boot

提问by Archit Aggarwal

I am facing the following error:

我面临以下错误:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationContextInitializer : org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:414) at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:394) at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:385) at org.springframework.boot.SpringApplication.initialize(SpringApplication.java:261) at org.springframework.boot.SpringApplication.(SpringApplication.java:237) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)

线程“main”中的异常 java.lang.IllegalArgumentException:无法实例化接口 org.springframework.context.ApplicationContextInitializer :org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:414) at 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)

My main method is

我的主要方法是

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);
    }
}

My pom.xml is

我的 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>

回答by Dibyajyoti Hazra

I recently came across this issue. I was running an executable jar through a bat file. My executable jar was already having all the spring boot jars required for the version mentioned in the pom.xml. But by mistake I defined a classpath of spring-boot libraries(of other version) in the bat file as well. When I removed that classpath entry, this particular issue was gone as then it was using only the spring boot jars that were mentioned in the pom file. I think this issue is coming because of the version conflict of the spring jars. Please correct me if I'm wrong. Thanks.

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

回答by M. Deinum

Your <dependencies>should look something like this

<dependencies>应该看起来像这样

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

And not the mixture of Spring Boot (1.3 and 1.4) versions and Spring Versions (4.0, 4.2 and 4.3) as you had. You also included dependencies that are already included (or break things).

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

Another thing is you should have the spring-boot-mavenplugin which is also lacking in your pom.

另一件事是您应该拥有spring-boot-mavenpom.xml 中也缺少的插件。

回答by Ece Kaya

It worked for me to change version of spring-boot-starter-parentfrom 2.0.0to 2.1.3.

将版本spring-boot-starter-parent2.0.0更改为2.1.3对我有用

回答by Oleksii Kyslytsyn

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);
}