Spring Boot的@Service和@Autowired和@ComponentScan注解

Spring Boot的@Service和@Autowired和@ComponentScan注解

SpringBoot项目的Bean装配默认规则是根据Application类(SpringBoot项目入口类)所在的包位置从上往下扫描,即只扫描该类所在的包及其子包。

1.如果需要扫描其上层包中的类,则将其移动到其上层包中

2.使用@ComponentScan注解,在SpringBoot启动类上注解配置,需要扫描的包。

SpringBootApplication
@ComponentScan(basePackages = {"com.example"})
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

猜你喜欢

转载自my.oschina.net/u/3537796/blog/1822849