Java 无法处理配置类的导入候选
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39375016/
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
Failed to process import candidates for configuration class
提问by bachr
I've a spring boot project that I can run successfully from within IntelliJ, but when I package an executable jar I can no longer run it. Here is the stack trace of the exception:
我有一个 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
My configuration is something like:
我的配置是这样的:
@Configuration
@EnableAutoConfiguration
public class AppConfig {
... some beans
}
I've added META-INF/spring.factories
under the project resources folder as described in 43.2 Locating auto-configuration candidatesas follows But this didn't fix the problem:
我META-INF/spring.factories
在项目资源文件夹下添加了如下43.2 定位自动配置候选者中所述但这并没有解决问题:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
el.dorado.AppConfig
Here is the project pom.xml
:
这是项目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>
采纳答案by bachr
I just figure it out, I should have been using Spring Boot maven plugininstead. Now the build section of my pom.xml
looks like:
我只是想通了,我应该改用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>
I build the project with mvn clean package
and then java -jar target/myproject.jar
and it works like a charm.
我用mvn clean package
然后构建项目,java -jar target/myproject.jar
它就像一个魅力。
回答by Cyanny
I solved it as follows:
我是这样解决的:
- Delete the
spring-boot-maven-plugin
- Config spring transformers in shade plugin please refer to spring parent pom
- 删除
spring-boot-maven-plugin
- 在 shade 插件中配置 spring 转换器请参考spring 父 pom
回答by Brad Mace
I've also gotten the
我也得到了
Failed to process import candidates for configuration class [...]; nested exception is java.lang.IllegalStateException: Unable to read meta-data for class ...
无法处理配置类 [...] 的导入候选;嵌套异常是 java.lang.IllegalStateException: Unable to read meta-data for class ...
error due to a typo in my spring.factories
file. In this case the root exception was
由于我的spring.factories
文件中的错字而导致的错误。在这种情况下,根异常是
class path resource [...] cannot be opened because it does not exist.
类路径资源 [...] 无法打开,因为它不存在。
This is an important spot to check since it can't be validated at compile time.
这是一个重要的检查点,因为它不能在编译时验证。
回答by Michael
I switched from
mvn clean compile assembly:single
to
mvn clean package
and the error went away.
我从mvn clean compile assembly:single
to
切换,
mvn clean package
错误消失了。
回答by susan097
I think that you have forgotten to use Annotation in your AppConfig.
我认为您忘记在 AppConfig 中使用 Annotation。
Add following three annotations to your AppConfig
class:
将以下三个注释添加到您的AppConfig
类中:
@Configuration
@EnableWebMvc
@ComponentScan("Scan_Package_Name")
public class AppConfig {
... some beans
}