首页 > 其他分享 >Spring IOC官方文档学习笔记(十)之类路径扫描与组件管理

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

时间:2023-02-13 22:26:00浏览次数:44  
标签:Service Spring springframework ComponentScan 文档 org 注解 IOC

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并将其注入进容器中,如下所示

//声明3个类,这三个类均位于cn.example.spring.boke同一个包下,其中ExampleA和ExampleB被注解标注,表明它们是bean,需要被注入进容器中,ExampleC是一个普通的类
@Service
public class ExampleA { }

@Component
public class ExampleB { }

public class ExampleC { }

//使用@Configuration注解定义一个配置类,在该配置类上,使用@ComponentScan注解指定包扫描路径为cn.example.spring.boke,这时,Spring就会扫描该包以及该包的子包下所有符合条件的bean,并将它们注入容器中,该注解等同于在之前提到过的<context:component-scan base-package="..."/>标签
@Configuration
@ComponentScan("cn.example.spring.boke")
public class Config { }

//启动容器,注意这里使用的是AnnotationConfigApplicationContext容器,而非之前的ClassPathXmlApplicationContext容器,而我们也不再需要xml配置文件,实现了完全的基于注解的配置
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
Arrays.stream(ctx.getBeanDefinitionNames()).forEach(System.out::println);

//打印结果如下,可以看到,容器扫描到了exampleA和exampleB以及我们的配置类config,而忽略了exampleC(其他的bean是Spring隐式自动添加的)
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
config
exampleA
exampleB

3.使用过滤器来自定义组件扫描规则

(1) 默认情况下,Spring仅检测被@Component、@Repository、@Service、@Controller、@Configuration和以@Component等作为元注解的自定义注解(如:@RestController)标注的类,不过,我们可以通过自定义@ComponentScan注解中的过滤器属性includeFilters和excludeFilters来修改这一默认行为,如下

//只修改上面例子中的配置类,此处假设我们只扫描被@Service注解标注的类,其自定义过滤规则如下
//includeFilters:要被注入的    excludeFilters:不要被注入的  @ComponentScan.Filter:指定过滤类型
@Configuration
@ComponentScan(basePackages = "cn.example.spring.boke",
                includeFilters = @ComponentScan.Filter(Component.class),
                excludeFilters = @ComponentScan.Filter(Service.class))
public class Config {
}

//输出结果如下,可见我们的exampleA已被排除在外
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
config
exampleB
exampleC

过滤类型:

过滤类型 说明
annotation(默认,按照注解过滤) 如上面例子中的属性excludeFilters = @ComponentScan.Filter(Service.class),就是排除掉所有被@Service注解标注的类
assignable( 按照类型过滤) 例如:excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ExampleA.class),就是排除掉ExampleA这个类型的bean
aspectj(按照切面表达式过滤) 例如:cn.example..*Service+,这是一个切入点表达式,会对所有符合这个切入点表达式的bean进行过滤
regex(按照正则表达式过滤) 同上,只不过满足的是正则表达式
custom(自定义过滤器) 实现org.springframework.core.type.TypeFilter接口来自定义过滤器,实现自定义过滤规则

(2) 设置@ComponentScan注解中的useDefaultFilters属性值为false,来禁用默认过滤器,即关闭Spring对于那些默认注解(如@Component等)的自动扫描检测

4.定义组件的配置元数据

未完待续...

标签:Service,Spring,springframework,ComponentScan,文档,org,注解,IOC
From: https://www.cnblogs.com/shame11/p/17117066.html

相关文章

  • Solon2 开发之IoC,九、自定义注解开发汇总
    1、定义构建能力注解,比如@Controller注解类:@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic@interfaceController{}注解类......
  • 如何理解spring框架中的依赖注入和控制反转?
     ioc,InversionofControl(控制反转),是Spring中的一种设计思想而非技术。我们可以从4个方面理解ioc:①谁控制谁?——Ioc容器控制对象。②控制了什么?——Ioc容器控......
  • springboot多模块项目搭建
    最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块。代码中的多模块是用maven管理的,每个模块都使用springboot框架。之前有......
  • day08-SpringMVC底层机制简单实现-04
    SpringMVC底层机制简单实现-04https://github.com/liyuelian/springmvc-demo.git8.任务7-完成简单视图解析功能说明:通过目标方法返回的String,转发或重定向到指定页面......
  • Spring Cloud Contract/Test
    Spring|Cloudhttps://spring.io/cloudApacheOpenWhiskisaserverless,opensourcecloudplatformhttps://openwhisk.apache.org/SpringCloudContracthttps:/......
  • 001.SpringIoc初体验
    1.创建entity(Apple、Child)packagecom.imooc.spring.ioc.entity;publicclassApple{privateStringtitle;privateStringcolor;privateStringori......
  • SpringCloud 集成 Consul+restTemplate 集群实战
      服务提供者应用两个模块(prot:8021,8021)pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmln......
  • Idea基于Gradle构建SpringBoot3.0集成MP项目
    发生缘由学习Idea基于Gradle构建SpringBoot3.0集成MP项目运行环境电脑系统:win10jdk:17.0.6JVM:JavaHotSpot(TM)64-BitServerVM(build17.0.6+9-LTS-190,mixed......
  • 七、SpringMVC异常处理机制
    异常处理的思路系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试等手段减少运行时异常的发生......
  • 如何理解Spring框架中的ioc?
    目录如何理解Spring框架中的ioc?DI耦合和内聚如何理解Spring框架中的ioc?ioc,InversionofControl(控制反转),是Spring中的一种设计思想而非技术。我们可以从4个方面理解io......