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

时间:2016-10-09 13:36:50

标签: java spring-boot

我遇到以下错误:

  

线程“main”中的异常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>

4 个答案:

答案 0 :(得分:0)

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

而不是Spring Boot(1.3和1.4)版本和Spring版本(4.0,4.2和4.3)的混合。您还包括已包含(或破坏事物)的依赖项。

另一件事是你应该拥有你的pom中缺少的spring-boot-maven插件。

答案 1 :(得分:0)

spring-boot-starter-parent的版本从 2.0.0 更改为 2.1.3 对我来说很有效。

答案 2 :(得分:0)

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

答案 3 :(得分:-1)

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