Scan components of various maven / JAR modules in Spring boot application

I have two Maven modules. The first, called the "application", contains the spring bootApplication class , which contains only these lines:

package org.example.application;

@SpringBootApplication
@ComponentScan({"org.example.model", "org.example"})
public class Application {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }
}

In the same Maven module and package org.example.application, I have RestControllerone that uses Component, which in turn uses the components of another Maven module, described below.

Another Maven module, called a "model", contains components spring boot(crud-repositories, entity, etc.). All these classes are in the same package structure as the first Maven ( org.example) module , but in subpackages of this, for example org.example.model.entities, org.example.model.repositoriesetc.

So the stream looks like this:

Maven module applicationin org.example package:
SpringBootApplication -> RestController -> MyComponent

, MyComponent, model Maven org.example.model.

, :

***************************
APPLICATION FAILED TO START
***************************

Description:

Field myRepository in org.example.MyComponent required a bean of type 'org.example.model.repositories.MyRepository' that could not be found.

Action:

Consider defining a bean of type 'org.example.model.repositories.MyRepository' in your configuration.

org.example.model.repositories.MyRepository Maven, SpringBootApplication!

, : @ComponentScan({"org.example.model", "org.example"}), , , .

?

+4
1

, , @ComponentScan, @SpringBootApplication - .
Spring :

@SpringBootApplication @Configuration, @EnableAutoConfiguration @ComponentScan

: , @ComponentScan , @SpringBootApplication, .

, org.example , org.example.model . , org.example .

:

@SpringBootApplication(scanBasePackages={"org.example"})
+6

Source: https://habr.com/ru/post/1665529/


All Articles