spring通过注解注册bean的方式+spring生命周期
spring容器通过注解注册bean的方式
- @ComponentScan + 组件标注注解 (@Component/@Service...)
@ComponentScan(value = "com.example.demo.annotation")
spring会将
com.example.demo.annotation目录下标注了spring能识别的注解的类注册为bean
@ComponentScan还可以指定排除和包含规则- excludeFilters: 指定排除规则,排除哪些组件
- includeFilters: 指定只需要包含哪些组件,需要设置 useDefaultFilters = false
- FilterType.ANNOTATION 基于注解过滤
- FilterType.ASSIGNABLE_TYPE : 基于给定的类型过滤
- ...
- FilterType.CUSTOM: 自定义规则过滤
@ComponentScan(value = "com.example.demo.annotation",
/*excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
},*/
includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Configuration.class}),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {TestController.class}),
@ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MyTypeFilter.class})
},useDefaultFilters = false
)
其中
CUSTOM自定义规则中的MyTypeFilter需要实现TypeFilter接口,举例如下public class MyTypeFilter implements TypeFilter {
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
String className = metadataReader.getClassMetadata().getClassName();
System.out.println("------>"+className);
if (className.contains("er")){
return true;
}
return false;
}
}
- @Bean (可以将第三方包中的类注册为bean)
@Bean
Person person() {
return new Person("zhang");
}
- @Import
@Import({Color.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
- 导入一个普通类,容器会自动注册这个组件,组件的id默认是类的全类名
- 导入
ImportSelector:返回需要注册的组件
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
return new String[]{"com.example.demo.annotation.bean.Red"};
}
}
- 导入
ImportBeanDefinitionRegistrar类
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
boolean b = registry.containsBeanDefinition("com.example.demo.annotation.bean.Color");
boolean b1 = registry.containsBeanDefinition("com.example.demo.annotation.bean.Red");
if (b && b1){
registry.registerBeanDefinition("rainBow",new RootBeanDefinition(RainBow.class));
}
}
}
- 使用spring提供的 FactoryBean
public class ColorFactoryBean implements FactoryBean<Color> {
@Override
public Color getObject() throws Exception {
return new Color();
} @Override
public Class<?> getObjectType() {
return Color.class;
}
} @Bean
ColorFactoryBean colorFactoryBean() {
return new ColorFactoryBean();
}
applicationContext.getBean("colorFactoryBean") 默认获取到的是FactoryBean调用getObject方法返回的对象
要获取FactoryBean本身,需要在id前面加个& (&colorFactoryBean)
当满足某种条件时才注册bean,使用@Conditional
举例:在windows和linux上分别注册不同的bean
@Conditional({WindowsConditional.class})
@Bean("windows")
Person person1() {
return new Person("windows");
}
@Bean("linux")
@Conditional({LinuxConditional.class})
Person person2() {
return new Person("linux");
}
public class WindowsConditional implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
Environment environment = conditionContext.getEnvironment();
String property = environment.getProperty("os.name");
return property.toLowerCase().contains("windows");
}
}
public class LinuxConditional implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
Environment environment = conditionContext.getEnvironment();
String property = environment.getProperty("os.name");
return property.toLowerCase().contains("linux");
}
}
bean的生命周期
spring容器管理bean的生命周期:创建--》初始化--》销毁
我们可以自定义初始化和销毁方法,容器在bean进行到当前生命周期时来调用我们自定义的初始化和销毁方法
创建对象:
单实例:在容器启动时创建对象
多实例:在每次获取bean的时候创建对象每个BeanPostProcessor的 postProcessBeforeInitialization 方法会在初始化之前执行
初始化: 对象创建好,调用初始化方法
每个BeanPostProcessor的 postProcessAfterInitialization 方法会在初始化之后执行销毁:
单实例:容器关闭时销毁
多实例:容器会帮助创建这个bean,但不会管理这个bean,所以容器不会调用销毁方法,可以手动调用销毁方法
指定初始化和销毁的方法:
- 在 @Bean注解指定(initMethod = "",destroyMethod = "")
- 通过让bean实现 InitializingBean(定义初始化逻辑) 和 DisposableBean(定义销毁时逻辑)
- 使用JSR250: @PostConstruct (定义初始化逻辑) @PreDestroy(在容器销毁bean之前通知进行清理工作)
bean的后置处理器 BeanPostProcessor,其有如下两个方法,在bean的初始化前后做一些处理:
- postProcessBeforeInitialization:在初始化之前工作
- postProcessAfterInitialization:在初始化之后工作
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
private final ApplicationContext applicationContext;
public MyBeanPostProcessor(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization--->"+beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization--->"+beanName);
return bean;
}
}
如果自定义组件想要使用spring容器底层的组件(ApplicationContext,BeanFactory,***),自定义组件可以实现 ***Aware,
在创建bean的时候,相关BeanPostProcessor会调用接口规定的方法注入相关组件
例如:
如果自定义bean 实现了ApplicationContextAware 接口,在ApplicationContextAwareProcessor中会调用ApplicationContextAware的
setApplicationContext方法,注入ApplicationContext组件
spring通过注解注册bean的方式+spring生命周期的更多相关文章
- @Scope注解设置创建bean的方式和生命周期
1.1.1 Scope注解创建bean的方式和生命周期 作用 Scope设置对象在spring容器(IOC容器)中的生命周期,也可以理解为对象在spring容器中的创建方式. 取 ...
- Spring框架系列(三)--Bean的作用域和生命周期
Bean的作用域 Spring应用中,对象实例都是在Container中,负责创建.装配.配置和管理生命周期(new到finalize()) Spring Container分为两种: 1.BeanF ...
- 五 Spring的配置:Bean的配置,生命周期和作用范围
Bean相关的配置: <bean>标签的id和name的配置: id:使用了约束中的唯一约束,里面不能出现特殊字符 name:没有使用唯一约束,理论上可以重复,实际上开发不行,里面可以出现 ...
- 【String注解驱动开发】面试官让我说说:如何使用FactoryBean向Spring容器中注册bean?
写在前面 在前面的文章中,我们知道可以通过多种方式向Spring容器中注册bean.可以使用@Configuration结合@Bean向Spring容器中注册bean:可以按照条件向Spring容器中 ...
- 【String注解驱动开发】如何按照条件向Spring容器中注册bean?这次我懂了!!
写在前面 当bean是单实例,并且没有设置懒加载时,Spring容器启动时,就会实例化bean,并将bean注册到IOC容器中,以后每次从IOC容器中获取bean时,直接返回IOC容器中的bean,不 ...
- spring IOC容器实例化Bean的方式与RequestContextListener应用
spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype 每次从容器中调用Bean时, ...
- 在Listener(监听器)定时启动的TimerTask(定时任务)中使用Spring@Service注解的bean
1.有时候在项目中需要定时启动某个任务,对于这个需求,基于JavaEE规范,我们可以使用Listener与TimerTask来实现,代码如下: public class TestTaskListene ...
- Spring中Bean的作用域、生命周期
Bean的作用域.生命周期 Bean的作用域 Spring 3中为Bean定义了5中作用域,分别为singleton(单例).protot ...
- Spring之Bean的作用域与生命周期
在前面博客中提到容器启动获得BeanDefinition对象中有一个scope 属性.该属性控制着bean对象的作用域.本章节介绍Bean的作用域及生命周期,了解bean是怎么来的又怎么没的. 一.B ...
随机推荐
- Java中的函数式编程(四)方法引用method reference
写在前面 我们已经知道,lambda表达式是一个匿名函数,可以用lambda表达式来实现一个函数式接口. 很自然的,我们会想到类的方法也是函数,本质上和lambda表达式是一样的,那是否也可以用类 ...
- 如何使用远程工具连接Linux服务器
大家好,今天我想和大家分享一下Linux如何连接远程控制工具我们都知道,Linux是著名的开源服务器操作系统,而在运维工程师的实际工作当中,我们不大可能时时刻刻都在服务器本地操作.因此这时,我们要用远 ...
- 改善深层神经网络-week1编程题(Regularization)
Regularization Deep Learning models have so much flexibility and capacity that overfitting can be a ...
- zuul的各种配置
我们知道我们前台要展示数据给用户看,这中间可能涉及到从后端的多个微服务进行获取数据.比如获取用户信息需要用到用户微服务.获取商品信息需要获取商品微服务.创建订单需要调用订单微服务,而各个微服务可能分布 ...
- 2021.9.17考试总结[NOIP模拟55]
有的考试表面上自称NOIP模拟,背地里却是绍兴一中NOI模拟 吓得我直接文件打错 T1 Skip 设状态$f_i$为最后一次选$i$在$i$时的最优解.有$f_i=max_{j<i}[f_j+a ...
- 使用Keil下载Hex文件进STM32
前言 初学STM32时,是通过串口1把Hex文件下载进STM32的,需要一个串口模块,而且还要设置BOOT0和BOOT1电平,然后通过FlyMcu软件进行下载,这也是一种不错的方法,这里我要介绍的是使 ...
- linux命令中find, which、whereis、locate,有什么区别?
whatis 用于查询一个命令执行什么功能,并将查询结果打印到终端上 which 查看可执行文件的位置 whereis 查看文件的位置 man Linux提供了丰富的帮助手册,当你需要查看某个命令的参 ...
- RocketMQ源码详解 | Broker篇 · 其二:文件系统
概述 在 Broker 的通用请求处理器将一个消息进行分发后,就来到了 Broker 的专门处理消息存储的业务处理器部分.本篇文章,我们将要探讨关于 RocketMQ 高效的原因之一:文件结构的良好设 ...
- clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
修改文件 /etc/sysconfig/nfs将#MOUNTD_PORT=892开启防火墙端口:firewalld-cmd --add-port=892/tcp
- Navicat15 For Mysql最新版完美破解图文教程(支持Win和Mac)
Navicat15 For Mysql最新版完美破解 欢迎关注博主公众号[跟着Mic学架构],专注于分享Java领域技术干货,回复关键字 [面试资料] 可以获得海量面试资料. 申明,本教程 Navic ...