Spring-Boot Executable-Jar NoSuchBeanDefinitionException [英] Spring-Boot Executable-Jar NoSuchBeanDefinitionException
问题描述
我有一个使用 Spring-Boot 创建的应用程序.
I have an application created using Spring-Boot.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
并将我的 Appplication.java 注释如下.
And have my Appplication.java annotated as shown below.
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application
public static void main(String[] args) {
final ConfigurableApplicationContext context = SpringApplication.run(Application.class);
...
当我在 eclipse 中运行它时它没有任何问题,但是当我生成一个可执行的 jar 时它会抛出异常
This works when I run it inside eclipse without any problems but, when I generate an executable jar it throws the exception
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.company.engine.services.BaseDao] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:319)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:987)
有什么想法吗?
pom.xml
<profiles>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.company.engine.Application</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
项目结构
com.company.engine
应用程序.java
com.company.engine.model
域.java
com.company.engine.service
CrudRepositoryJavaClasses
CrudRepositoryJavaClasses
来自@Oleg Majewski 的解决方案,稍作修改:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.1.8.RELEASE</version>
<configuration>
<mainClass>com.company.engine.Application</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
推荐答案
你可以尝试使用 spring-boot-maven-plugin 而不是自己做 maven-assembly-plugin 和 maven-dependency-plugin :
You can try to use spring-boot-maven-plugin instead of doing maven-assembly-plugin and maven-dependency-plugin by yourself:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.1.8.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
在你可以像这样构建 jar 之后:
After you can build the jar like this:
$ mvn package
$ ls target/*.jar
这篇关于Spring-Boot Executable-Jar NoSuchBeanDefinitionException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!