Slava
Slava

Reputation: 155

spring boot jar NoSuchBeanDefinitionException

I builded spring boot application which works well if I start it with mvn spring-boot:run but trying to run it java -jar jarName.jar results in error Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'HomeService' available

here is my pom.xml with spring-boot-maven-plugin like it is described here https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html and here 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>

here is 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());

}

here is HomeService

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

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

I tried also to get a bean in @PostConstruct method, where everything and all Autowires should be ready, it also does not work.

What is the difference between mvn spring-boot:run and mvn package ? How could I build correct jar?

Thanks!

Upvotes: 2

Views: 782

Answers (2)

Slava
Slava

Reputation: 155

I solved my problem. It is confusing, so I will try to figure out why it is so. To make jar run I should remove (basePackages = { "myProject.status" })

@ComponentScan (basePackages = { "myProject.status" }) works with mvn spring-boot:run but only pure @ComponentScan works from jar. Actually it is really my root directory, so I do not need it, but the question, why does not it work from jar is opened. All classes are under this root directory, so it should find it there but maybe it does not scan any dependencies running from jar.

Thank you all, who replied to me!

Upvotes: 1

chenrui
chenrui

Reputation: 9866

You can just use $ mvn package spring-boot:repackage to build a standalone executable jar.

The difference between mvn spring-boot:run and mvn spring-boot:repackage is that spring-boot:run for local run and debug, while spring-boot:repackage is to package for deployment.

Also, you need to specify <packaging>jar</packaging>.

Another comment for App.java, you can just use one annotation as @SpringBootApplication. It would cover all your three annotations, assuming App.java is in Package myProject.status.

Upvotes: 2

Related Questions