在jar中构建后,Spring应用程序无法正常工作

时间:2017-01-27 19:41:07

标签: java spring intellij-idea

我有一个简单的Spring应用程序:

主要课程:

pending

预定职位的课程

@SpringBootApplication
@EnableScheduling
public class SchedulerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SchedulerApplication.class, args);
    }
}

这是pom.xml

@Component
public class Executor {

    private static final Logger log = LoggerFactory.getLogger(Executor.class);
    private Integer jobCounter = 1;

    @Scheduled(fixedDelay = 1000)
    public void run() {
        log.info("Start task (" + jobCounter + ")");
        log.info("Stop task");
        jobCounter ++;
    }
}

这是我的项目结构

enter image description here

它在IDE(IntelliJ IDEA)下工作正常,但当我将此代码编译为jar文件并运行时:

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ru.alexeyzhulin</groupId>
    <artifactId>scheduler</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>scheduler</name>
    <description>Task scheduler</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.4.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</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

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


</project>

我收到了很多这样的错误

org.springframework.beans.factory.BeanDefinitionStoreException:无法处理配置类的导入候选[ru.alexeyzhulin.SchedulerApplication];嵌套异常是java.lang.IllegalArgumentException:在META-INF / spring.f actor中找不到自动配置类。如果您使用的是自定义包装,请确保该文件正确无误。

我错过了什么?

2 个答案:

答案 0 :(得分:0)

在pom。

中的sprig boot maven插件下添加以下元素
<configuration>
  <fork>true</fork>
  <mainClass>Your-main-config-class</mainClass>
</configuration>

答案 1 :(得分:0)

您的SpringBootApplication类似乎位于错误的位置。 它应该正确定位。通常它位于工件包的根目录中。 例如:

ru.alexeyzhulin.scheduler
   ru.alexeyzhulin.scheduler.SchedulerApplication
   ru.alexeyzhulin.scheduler.Executor

ru.alexeyzhulin.scheduler
   ru.alexeyzhulin.scheduler.SchedulerApplication
ru.alexeyzhulin.scheduler.components
   ru.alexeyzhulin.scheduler.components.Executor 

看看它: Failed to process import candidates for configuration class