设为首页 加入收藏

TOP

Spring IOC官方文档学习笔记(十)之类路径扫描与组件管理(一)
2023-07-26 08:15:56 】 浏览:73
Tags:Spring 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),就是排除掉所有被@Ser
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇如何理解spring框架中的依赖注入.. 下一篇springboot自动配置原理以及sprin..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目