Spring boot search on another package

I am developing a Spring boot application that uses some Spring repository interfaces:

package test; @SpringBootApplication public class Application implements CommandLineRunner { @Autowired private BookRepository repository; . . . } 

I see that the BookRepository interface (which follows here) can only be entered if it is in the same package as the Application class:

 package test; public interface BookRepository extends MongoRepository<Book, String> { public Book findByTitle(String title); public List<Book> findByType(String type); public List<Book> findByAuthor(String author); } 

Is there any Spring boot annotation that I can apply in my classes to find BookRepository in another package?

+6
source share
2 answers

Use the @ComponentScan annotation together with @SpringBootApplication and configure a custom base package (you can specify a list of package names or a list of classes whose package will be used), for example

 @SpringBootApplication @ComponentScan(basePackages = {"otherpackage", "..."}) public class Application 

or

 @SpringBootApplication @ComponentScan(basePackageClasses = {otherpackage.MyClass.class, ...}) public class Application 

Please note that component checking will find classes inside and below the specified packages.

+14
source

It’s good to check the scope of classes stored in different packages using the @ComponentScan annotation in Spring boot custom load class.

Also add @Component in modal classes that are used to allow access to classes.

An example is stored at http://www.javarticles.com/2016/01/spring-componentscan-annotation-example.html

0
source

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


All Articles