Spring-Boot Executable-Jar NoSuchBeanDefinitionException

时间:2014-11-17 19:01:27

标签: java spring-boot

我有一个使用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注释如下所示。

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application

public static void main(String[] args) {
    final ConfigurableApplicationContext context = SpringApplication.run(Application.class);
...

当我在eclipse中运行它时没有任何问题,但是,当我生成一个可执行jar时,它会抛出异常

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

Application.java

com.company.engine.model

Domain.java

com.company.engine.service

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>

1 个答案:

答案 0 :(得分:2)

你可以尝试使用spring-boot-maven-plugin而不是自己做maven-assembly-plugin和maven-dependency-plugin:

<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之后:

$ mvn package
$ ls target/*.jar