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

Posted

技术标签:

【中文标题】无法处理配置类的导入候选【英文标题】:Failed to process import candidates for configuration class 【发布时间】:2017-01-15 10:43:09 【问题描述】:

我有一个可以在 IntelliJ 中成功运行的 Spring Boot 项目,但是当我打包一个可执行 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

我已经在项目资源文件夹下添加了META-INF/spring.factories,如43.2 Locating auto-configuration candidates 中所述,如下但这并没有解决问题:

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 可以分享一下SpringBootApplication类和pom.xml吗 这是一个奇怪的设置。一方面,您不应该将@EnableAutoConfiguration 放在自动配置类中。请查看您引用的文档。其次,您似乎正在尝试在类路径上没有spring-boot-autoconfigure 的情况下运行您的应用程序。没有找到spring.factories,因此您的构建没有正确打包它。请分享一个重现问题的项目。 @StephaneNic​​oll 我想我必须在 spring-boot 某处添加@EnableAutoConfiguration 以配置其他bean(例如JPA)。我已经用项目pom.xml 更新了这个问题。另外,当我使用jar tf jar-file 检查生成的jar 时,我实际上看到META-INF/spring.factories 存在,所以我猜构建已正确打包! 我不知道您要做什么,但看起来您根本不需要自动配置。你为什么要创建META-INF/spring.factories 【参考方案1】:

我只是想通了,我应该改用Spring Boot maven plugin。现在我的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,它就像一个魅力。

【讨论】:

【参考方案2】:

我认为您忘记在 AppConfig 中使用 Annotation。

AppConfig 类中添加以下三个注解:

@Configuration
@EnableWebMvc
@ComponentScan("Scan_Package_Name") 
public class AppConfig 
  ... some beans

【讨论】:

【参考方案3】:

我也买了

未能处理配置类的导入候选 [...];嵌套异常是 java.lang.IllegalStateException: Unable to read meta-data for class ...

由于我的 spring.factories 文件中的拼写错误而导致错误。在这种情况下,根异常是

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

这是一个重要的检查点,因为它无法在编译时进行验证。

【讨论】:

【参考方案4】:

我从 mvn clean compile assembly:singlemvn clean package 并且错误消失了。

【讨论】:

【参考方案5】:

我是这样解决的:

    删除spring-boot-maven-plugin 在shade插件中配置spring transformers请参考spring parent pom

【讨论】:

【参考方案6】:

遇到了同样的问题。如果你也在使用 tomcat,那么清理它对我来说就可以了。

【讨论】:

以上是关于无法处理配置类的导入候选的主要内容,如果未能解决你的问题,请参考以下文章

解读SpringBoot和SpringMVC中配置类的@Impot等导入是如何解析的

解读SpringBoot和SpringMVC中配置类的@Impot等导入是如何解析的

spring boot 导入xml配置文件所需注解和禁用自动配置类的注解

【Spring】简述@Configuration配置类注册BeanDefinition到Spring容器的过程

Intellij idea 无法识别 python 3 中本地类的导入 [重复]

Spring -- Spring配置数据源(连接池)& 快速获取类的全限定类名