spring boot jar NoSuchBeanDefinitionException

标签 spring maven spring-boot jar executable-jar

我构建了 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:runmvn package 之间有什么区别?我怎样才能构建正确的jar?

谢谢!

最佳答案

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

mvn spring-boot:run之间的区别和mvn spring-boot:repackagespring-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/

相关文章:

maven - NoClassDefFoundError:org/json/JSONObject - Hadoop MapReduce

Maven 发布 :prepare : Cannot prepare the release because you have local modifications

spring boot打包后找不到资源文件

java - 尽管存在 @Bean,@Autowired RabbitTemplate 仍为 null

spring - JMeter性能测试下出现Activiti死锁

spring - 跨域不起作用

第一次使用maven后出现Java.lang.NoClassDefFoundError?

spring-boot - Spring Boot + Spring 集成 Java DSL + AOP : Fails to proxy the Gateway interface

spring-boot - spring 数据 redis 循环引用

java - 打包的 Spring boot 应用程序无法将其余 api 错误解析为消息