无法从位置加载工厂[META-INF / spring.factories]

时间:2018-04-24 05:55:35

标签: java spring-boot

我使用Spring Initializr创建了spring boot应用程序,它生成在pom.xml下面

.....
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

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

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>        
</dependencies>

<build>
    <finalName>testproject</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

但是当我试图让它运行时低于错误。我不确定为什么它在spring.factories文件中失败。

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.IllegalArgumentException: Unable to load factories from location [META-INF/spring.factories]
    at org.springframework.core.io.support.SpringFactoriesLoader.loadSpringFactories(SpringFactoriesLoader.java:147)
    at org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(SpringFactoriesLoader.java:119)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:426)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:418)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:266)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:247)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)
    at 
    ... 8 more
Caused by: java.util.zip.ZipException: invalid distance too far back
    at java.util.zip.InflaterInputStream.read(Unknown Source)
    at org.springframework.boot.loader.jar.ZipInflaterInputStream.read(ZipInflaterInputStream.java:52)
    at java.io.FilterInputStream.read(Unknown Source)
    at java.util.Properties$LineReader.readLine(Unknown Source)
    at java.util.Properties.load0(Unknown Source)
    at java.util.Properties.load(Unknown Source)
    at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:140)
    at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:122)
    at org.springframework.core.io.support.SpringFactoriesLoader.loadSpringFactories(SpringFactoriesLoader.java:136)
    ... 16 more

我的应用程序很简单,没有任何配置,我不确定我需要添加到spring.factories文件中。

2 个答案:

答案 0 :(得分:0)

当使用spring初始化程序创建相同项目时,它适用于我。

您没有指定尝试启动项目的方式,请尝试以下操作:

mvn clean install
mvn spring-boot:run

来自命令行。

答案 1 :(得分:0)

SpringFactoriesLoader尝试加载spring.factories但无法从META-INF/spring.factories找到该文件。由于spring.factories用于Auto-configured Beans,您需要检查是否使用了@EnableAutoConfiguration@ConditionalOnClass@ConditionalOnMissingBean种注释。确保您知道正在使用的注释。

解决方法是在spring.factories文件夹下放置一个空白文件META-INF