spring boot jar NoSuchBeanDefinitionException

时间:2017-12-08 11:10:09

标签: spring maven spring-boot jar executable-jar

我构建了spring boot应用程序,如果我使用mvn spring-boot:run启动它,但尝试运行它java -jar jarName.jar会导致错误Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'HomeService' available

这是我的pom.xml spring-boot-maven-plugin,就像这里描述的那样https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.htmlSpring-Boot Executable-Jar NoSuchBeanDefinitionException

    <build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <directory>src/main/resources/${profileName}</directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArgument>-proc:none</compilerArgument>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <version>2.2.4</version>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <!-- source output directory -->
                        <outputDirectory>target/metamodel</outputDirectory>
                        <!-- <processors> -->
                        <!-- <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor> -->
                        <!-- </processors> -->
                        <overwrite>true</overwrite>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.5.8.RELEASE</version>
            <configuration>
                <executable>true</executable>
                <profiles>
                    <profile>dev</profile>
                    <profile>live</profile>
                </profiles>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>live</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                            <configuration>
                                <tasks>
                                    <delete file="src/main/resources/application.properties" />
                                    <copy file="src/main/resources/application-live.properties"
                                        tofile="src/main/resources/application.properties" />
                                </tasks>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>dev</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                            <configuration>
                                <tasks>
                                    <delete file="src/main/resources/application.properties" />
                                    <copy file="src/main/resources/application-dev.properties"
                                        tofile="src/main/resources/application.properties" />
                                </tasks>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

这是App class

@Configuration 
@EnableAutoConfiguration
@ComponentScan (basePackages = { "myProject.status" })
public class App {

private static final Logger logger = LogManager.getLogger(App.class);

public static void main(String[] args) throws BeansException, IOException, InterruptedException {
    logger.info("APPLICATION STARTED");

    ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
    logger.info(context.getBean(HomeService.class).home());

}

这是HomeService

package myProject.status.service;
@Service
public class HomeService {

    public String home() {
        return "application works";
    }
}

我还尝试在@PostConstruct方法中获取一个bean,其中所有和所有Autowires都应该准备就绪,它也不起作用。

mvn spring-boot:runmvn package之间有什么区别?我怎么能建立正确的罐子?

谢谢!

2 个答案:

答案 0 :(得分:2)

您可以使用$ mvn package spring-boot:repackage构建一个独立的可执行jar。

mvn spring-boot:runmvn spring-boot:repackage之间的区别在于spring-boot:run用于本地运行和调试,而spring-boot:repackage用于打包以进行部署。

此外,您需要指定<packaging>jar</packaging>

App.java的另一个评论,您可以使用一个注释@SpringBootApplication。假设App.java位于包myProject.status中,它将覆盖您的所有三个注释。

答案 1 :(得分:1)

我解决了我的问题。这很令人困惑,所以我会试着找出原因。为了让jar运行,我应该删除(basePackages = { "myProject.status" })

@ComponentScan (basePackages = { "myProject.status" })适用于mvn spring-boot:run,但只有纯@ComponentScan适用于jar。实际上它确实是我的根目录,所以我不需要它,但问题是,为什么它不能从jar工作打开。所有类都在这个根目录下,所以它应该在那里找到它,但它可能不会扫描从jar运行的任何依赖项。

谢谢大家,谁回复了我!