无法处理配置类的导入候选项

27

我有一个Spring Boot项目,可以在IntelliJ中成功运行,但是当我打包成可执行的jar文件后,无法再运行。以下是异常的堆栈跟踪:

18:13:55.254 [main] INFO  o.s.c.a.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@b3d7190: startup date [Wed Sep 07 18:13:55 CEST 2016]; root of context hierarchy
18:13:55.403 [main] WARN  o.s.c.a.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [el.dorado.App]; nested exception is java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.
18:13:55.414 [main] ERROR o.s.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [el.dorado.App]; nested exception is java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.
    at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:489)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:191)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:321)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)
    at dz.lab.jpmtask.App.main(App.java:33)
Caused by: java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.
    at org.springframework.util.Assert.notEmpty(Assert.java:276)
    at org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.getCandidateConfigurations(EnableAutoConfigurationImportSelector.java:145)
    at org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.selectImports(EnableAutoConfigurationImportSelector.java:84)
    at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:481)
    ... 13 common frames omitted

我的配置大致如下:

@Configuration
@EnableAutoConfiguration
public class AppConfig {
  ... some beans
}

按照43.2 查找自动配置的位置描述,在项目资源文件夹下添加META-INF/spring.factories文件。但是这并没有解决问题:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
el.dorado.AppConfig

这里是项目的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>el.dorado</groupId>
  <artifactId>ElDorado</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ElDorado</name>
  <url>http://maven.apache.org</url>

  <properties>
     <junit.version>4.12</junit.version>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>el.dorado.App</mainClass>
              <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <!--<version>0.7.8-SNAPSHOT</version>-->
        <executions>
          <execution>
            <id>jacoco-initialize</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>jacoco-site</id>
            <phase>verify</phase>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

你需要在入口点配置类上使用@EnableAutoConfiguration,以便让Boot首先扫描这个AppConfig - chrylis -cautiouslyoptimistic-
这是一个奇怪的设置。首先,您不应该在自动配置类上放置@EnableAutoConfiguration。请查看您引用的文档。其次,看起来您正在尝试在没有spring-boot-autoconfigure的情况下运行应用程序。没有找到spring.factories,因此您的构建未正确打包它。请分享一个可以重现问题的项目。 - Stephane Nicoll
@StephaneNicoll 我以为我需要在spring-boot中添加@EnableAutoConfiguration来配置其他bean(例如JPA)。我已经使用项目的pom.xml更新了问题。此外,当我使用jar tf jar-file检查生成的jar时,我实际上看到META-INF/spring.factories存在,所以我想构建已经正确打包! - bachr
1
我不知道你在试图做什么,但看起来你根本不需要自动配置。为什么你要一开始就创建一个META-INF/spring.factories呢? - Stephane Nicoll
@StephaneNicoll 当我删除文件时,出现了相同的异常。 - bachr
显示剩余2条评论
6个回答

14

我刚刚明白,我应该使用Spring Boot Maven插件。现在,我的pom.xml的构建部分看起来像这样:

<build>
<plugins>
  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
      <fork>true</fork>
      <mainClass>dz.lab.jpmtask.App</mainClass>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>repackage</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <!--<version>0.7.8-SNAPSHOT</version>-->
    <executions>
      <execution>
        <id>jacoco-initialize</id>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
      </execution>
      <execution>
        <id>jacoco-site</id>
        <phase>verify</phase>
        <goals>
          <goal>report</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
</build>

我使用mvn clean package构建项目,然后使用java -jar target/myproject.jar运行它,效果非常好。


1
我认为您忘记在AppConfig中使用注释了。
请在您的AppConfig类中添加以下三个注释:
@Configuration
@EnableWebMvc
@ComponentScan("Scan_Package_Name") 
public class AppConfig {
  ... some beans
}

0

我也遇到了以下错误:

无法处理配置类[...]的导入候选项;嵌套异常是java.lang.IllegalStateException:无法读取类的元数据...

这是由于我的spring.factories文件中有一个拼写错误导致的。在这种情况下,根本原因是:

类路径资源[...]无法打开,因为它不存在。

这是一个重要的检查点,因为它不能在编译时验证。


0
我是这样解决的:
  1. 删除 spring-boot-maven-plugin
  2. 在 shade 插件中配置 spring 转换器,请参考 spring parent pom

0

我从 mvn clean compile assembly:single 切换到 mvn clean package 然后错误消失了。


0

我遇到了同样的问题。如果你也在使用Tomcat,清理它对我很有帮助。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接