Spring-Starter pom.xml找不到Application启动类

时间:2014-10-04 08:52:36

标签: java spring maven spring-mvc pom.xml

我刚开始尝试使用pom.xml编写要执行的mvn exec:java文件。

<?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>com.satisfeet</groupId>
    <artifactId>app</artifactId>
    <version>0.0.0</version>

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

    <properties>
        <start-class>com.satisfeet.Application</start-class>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

我的目录结构是:

/src
  |_ /main
      |__ /java
           |__ /com
                |__ /satisfeet
                     |__ /core
                     |__ /http
                     |__ Application.java

当我现在运行mvn packagemvn exec:java时,我首先收到警告:

java.lang.ClassNotFoundException: com.satisfeet.Application
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
    at java.lang.Thread.run(Thread.java:745)

然后导致:

Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project app: An exception occured while executing the Java class. com.satisfeet.Application -> [Help 1]

我不明白,因为Application.java文件应该是正确的:

package com.satisfeet;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;

import com.satisfeet.http.CustomerController;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

我错过了什么?

1 个答案:

答案 0 :(得分:0)

看看Spring docs关于如何使用@ComponentScan - 你应该给它param,这是你想要Spring扫描的包。

package com.satisfeet;

@Configuration
@ComponentScan("com.satisfeet")
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}