Spring IOC官方文档学习笔记(十)之类路径扫描与组件管理

[复制链接]
发表于 2023-2-14 00:11:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
1.@Component注解与其衍生注解
(1) 在Spring中,@Component注解用于说明某个类是一个bean,之后Spring在类路径扫描过程中会将该bean添加至容器中;@Component注解还有很多衍生注解,如@Repository, @Service和@Controller,它们分别用于三层架构中的持久层,业务层和控制层,因此,对于一个业务层的普通Service类,一般情况下用@Component或@Service都是可行的,但更推荐用@Service注解,因为该注解不仅能清晰的指明被标注的类是一个业务类,此外,这些注解还是Spring AOP的理想切入目标,能方便的对某一层进行切入(例如:切入控制层,进行权限校验)
(2) Spring提供的许多注解都可以用作元注解,如Spring的@RestController注解,它就是将@Controller和@ResponseBody注解组合出来的一个新注解,使用该注解,在我们期望类中所有方法都返回json格式数据时,就不用在每一个方法上都标注@ResponseBody注解了,因此,Spring推荐我们根据业务需要,结合元注解来实现自定义注解
2.自动扫描并注册bean
(1) 上文我们提到了@Component注解等,那么,Spring该如何扫描到这些注解并将它们注入进容器中呢? 这就需要类路径扫描注解@ComponentScan了,我们需要把它添加到有@Configuration注解标注的配置类上,在容器启动后,Spring就会扫描指定包下的bean并将其注入进容器中,如下所示
  1. //声明3个类,这三个类均位于cn.example.spring.boke同一个包下,其中ExampleA和ExampleB被注解标注,表明它们是bean,需要被注入进容器中,ExampleC是一个普通的类
  2. @Service
  3. public class ExampleA { }
  4. @Component
  5. public class ExampleB { }
  6. public class ExampleC { }
  7. //使用@Configuration注解定义一个配置类,在该配置类上,使用@ComponentScan注解指定包扫描路径为cn.example.spring.boke,这时,Spring就会扫描该包以及该包的子包下所有符合条件的bean,并将它们注入容器中,该注解等同于在之前提到过的<context:component-scan base-package="..."/>标签
  8. @Configuration
  9. @ComponentScan("cn.example.spring.boke")
  10. public class Config { }
  11. //启动容器,注意这里使用的是AnnotationConfigApplicationContext容器,而非之前的ClassPathXmlApplicationContext容器,而我们也不再需要xml配置文件,实现了完全的基于注解的配置
  12. AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
  13. Arrays.stream(ctx.getBeanDefinitionNames()).forEach(System.out::println);
  14. //打印结果如下,可以看到,容器扫描到了exampleA和exampleB以及我们的配置类config,而忽略了exampleC(其他的bean是Spring隐式自动添加的)
  15. org.springframework.context.annotation.internalConfigurationAnnotationProcessor
  16. org.springframework.context.annotation.internalAutowiredAnnotationProcessor
  17. org.springframework.context.annotation.internalCommonAnnotationProcessor
  18. org.springframework.context.event.internalEventListenerProcessor
  19. org.springframework.context.event.internalEventListenerFactory
  20. config
  21. exampleA
  22. exampleB
复制代码
3.使用过滤器来自定义组件扫描规则
(1) 默认情况下,Spring仅检测被@Component、@Repository、@Service、@Controller、@Configuration和以@Component等作为元注解的自定义注解(如:@RestController)标注的类,不过,我们可以通过自定义@ComponentScan注解中的过滤器属性includeFilters和excludeFilters来修改这一默认行为,如下
  1. //只修改上面例子中的配置类,此处假设我们只扫描被@Service注解标注的类,其自定义过滤规则如下
  2. //includeFilters:要被注入的    excludeFilters:不要被注入的  @ComponentScan.Filter:指定过滤类型
  3. @Configuration
  4. @ComponentScan(basePackages = "cn.example.spring.boke",
  5.                 includeFilters = @ComponentScan.Filter(Component.class),
  6.                 excludeFilters = @ComponentScan.Filter(Service.class))
  7. public class Config {
  8. }
  9. //输出结果如下,可见我们的exampleA已被排除在外
  10. org.springframework.context.annotation.internalConfigurationAnnotationProcessor
  11. org.springframework.context.annotation.internalAutowiredAnnotationProcessor
  12. org.springframework.context.annotation.internalCommonAnnotationProcessor
  13. org.springframework.context.event.internalEventListenerProcessor
  14. org.springframework.context.event.internalEventListenerFactory
  15. config
  16. exampleB
  17. exampleC
复制代码
过滤类型:
过滤类型说明annotation(默认,按照注解过滤)如上面例子中的属性excludeFilters = @ComponentScan.Filter(Service.class),就是排除掉所有被@Service注解标注的类assignable( 按照类型过滤)例如:excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ExampleA.class),就是排除掉ExampleA这个类型的beanaspectj(按照切面表达式过滤)例如:cn.example..*Service+,这是一个切入点表达式,会对所有符合这个切入点表达式的bean进行过滤regex(按照正则表达式过滤)同上,只不过满足的是正则表达式custom(自定义过滤器)实现org.springframework.core.type.TypeFilter接口来自定义过滤器,实现自定义过滤规则(2) 设置@ComponentScan注解中的useDefaultFilters属性值为false,来禁用默认过滤器,即关闭Spring对于那些默认注解(如@Component等)的自动扫描检测
4.定义组件的配置元数据
未完待续...
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表