@ComponentScan注解是什么
https://blog.51cto.com/4247649/2118342
@ComponentScan注解默认就会装配标识了@Controller,@Service,@Repository,@Component注解的类到spring容器中
总结一下@ComponentScan的常用方式如下
-
自定扫描路径下边带有@Controller,@Service,@Repository,@Component注解加入spring容器
-
通过includeFilters加入扫描路径下没有以上注解的类加入spring容器
-
通过excludeFilters过滤出不用加入spring容器的类
-
自定义增加了@Component注解的注解方式
@ComponentScan的功能其实就是自动扫描并加载符合条件的组件或bean定义,最终将这些bean定义加载到容器中。我们可以通过basePackages等属性指定@ComponentScan自动扫描的范围,如果不指定,则默认Spring框架实现从声明@ComponentScan所在类的package进行扫描,默认情况下是不指定的,所以SpringBoot的启动类最好放在root package下。
@mapperScan是扫描mapper类的注解。springboot启动类上添加。mybatis映射
发现@SpringBootApplication
是一个复合注解,包括
@ComponentScan
,
@SpringBootConfiguration
,
@EnableAutoConfiguration
。
修改启动类,@SpringBootApplication(scanBasePackages = "com.zhihao.miao"),指定扫描路径:
可以使用@SpringBootApplication的另外二个参数(exclude或excludeName)
@SpringBootApplication(exclude=People.class)