我构建了 Spring Boot 应用程序,如果我使用 mvn spring-boot:run
启动它,该应用程序运行良好,但尝试运行它 java -jar jarName.jar
会导致错误由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“HomeService”类型的合格 bean
这是我的 pom.xml 和 spring-boot-maven-plugin
就像这里描述的 https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html在这里Spring-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>
这是应用程序类
@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:run
和 mvn package
之间有什么区别?我怎样才能构建正确的jar?
谢谢!
最佳答案
您可以使用 $ mvn package spring-boot:repackage构建一个独立的可执行 jar。
mvn spring-boot:run
之间的区别和mvn spring-boot:repackage
是spring-boot:run
用于本地运行和调试,而 spring-boot:repackage
就是打包部署。
此外,您需要指定<packaging>jar</packaging>
.
另一条评论App.java
,您可以只使用一个注释作为 @SpringBootApplication 。它将涵盖所有三个注释,假设 App.java
在包装中myProject.status
.
关于spring boot jar NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47713169/