springboot

@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(scanBasePackageClasses = MyConfig.class),指定scanBasePackageClasses参数的value值是你需要扫描的类也可以,结果一样,不过如果多个配置类不在当前包及其子包下,则需要指定多个。
排除:
在上面的列子的相同包下(com.zhihao.miao.springboot)配置了People,并将其纳入到spring容器中(@Component),我们知道@SpringBootApplication注解会扫描当前包及其子包,所以People类会纳入到spring容器中去,我们需要将其排除在spring容器中,如何操作?
可以使用@SpringBootApplication的另外二个参数(exclude或excludeName)
@SpringBootApplication(excludeName = {"com.zhihao.miao.springboot.People"})

@SpringBootApplication(exclude=People.class) 

posted on 2019-07-23 15:13  LittleSpring  阅读(189)  评论(0编辑  收藏  举报