Spring boot find autowired on another package

前端 未结 2 1260
猫巷女王i
猫巷女王i 2021-01-07 18:08

I\'m developing a Spring Boot application which uses some Spring Data Repository interfaces:

package test;
@SpringBootApplication
public class Application im         


        
相关标签:
2条回答
  • 2021-01-07 18:34

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

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

    or

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

    or since Spring 1.3.0 (Dec. 2016), you can directly write:

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

    Note that component scan will find classes inside and below the given packages.

    0 讨论(0)
  • 2021-01-07 18:38

    Good to verify the scopes of classes kept in different packages by using @ComponentScan annotation in Spring boot startup custom class.

    Also add @Component in modal classes being used to allow framework accessing the classes.

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

    0 讨论(0)
提交回复
热议问题