Spring容器中添加bean的5种方式
作者:楚三木 发布时间:2023-03-23 03:03:15
目录
@Configuration + @Bean
@Componet + @ComponentScan
@Import注解导入
@Import直接导入类
@Import + ImportSelector
@Import + ImportBeanDefinitionRegistrar
@Import + DeferredImportSelector
使用FactoryBean接口
使用 BeanDefinitionRegistryPostProcessor
小结
我们知道平时在开发中使用Spring的时候,都是将对象交由Spring去管理,那么将一个对象加入到Spring容器中,有哪些方式呢,下面我就来总结一下
@Configuration + @Bean
这种方式其实,在上一篇文章已经介绍过了,也是我们最常用的一种方式,@Configuration用来声明一个配置类,然后使用 @Bean 注解,用于声明一个bean,将其加入到Spring容器中。
具体代码如下:
@Configuration
public class MyConfiguration {
@Bean
public Person person() {
Person person = new Person();
person.setName("spring");
return person;
}
}
@Componet + @ComponentScan
这种方式也是我们用的比较多的方式,@Componet中文译为组件,放在类名上面,然后@ComponentScan放置在我们的配置类上,然后可以指定一个路径,进行扫描带有@Componet注解的bean,然后加至容器中。
具体代码如下:
@Component
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
}
@ComponentScan(basePackages = "it.chusen.spring.*")
public class Demo1 {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);
Person bean = applicationContext.getBean(Person.class);
System.out.println(bean);
}
}
结果输出:
Person{name='null'}
表示成功将Person放置在了IOC容器中。
@Import注解导入
前两种方式,大家用的可能比较多,也是平时开发中必须要知道的,@Import注解用的可能不是特别多了,但是也是非常重要的,在进行Spring扩展时经常会用到,它经常搭配自定义注解进行使用,然后往容器中导入一个配置文件。关于@Import注解,我会多介绍一点,它有三种使用方式,我会一一介绍。这是@Import注解的源码,表示只能放置在类上。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
/**
* 用于导入一个class文件
* {@link Configuration @Configuration}, {@link ImportSelector},
* {@link ImportBeanDefinitionRegistrar}, or regular component classes to import.
*/
Class<?>[] value();
}
@Import直接导入类
代码示例如下:
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
}
/**
* 直接使用@Import导入person类,然后尝试从applicationContext中取,成功拿到
**/
@Import(Person.class)
public class Demo1 {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);
Person bean = applicationContext.getBean(Person.class);
System.out.println(bean);
}
}
上述代码直接使用@Import导入了一个类,然后自动的就被放置在IOC容器中了。注意
我们的Person类上 就不需要任何的注解了,直接导入即可。
@Import + ImportSelector
其实在@Import注解的源码中,说的已经很清楚了,感兴趣的可以看下,我们实现一个ImportSelector的接口,然后实现其中的方法,进行导入。
代码如下:
@Import(MyImportSelector.class)
public class Demo1 {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);
Person bean = applicationContext.getBean(Person.class);
System.out.println(bean);
}
}
class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"it.chusen.spring.model.Person"};
}
}
我自定义了一个 MyImportSelector 实现了 selectImports 方法,然后将我们要导入的类的全限定名写在里面即可,实现起来也是非常简单。
@Import + ImportBeanDefinitionRegistrar
这种方式也需要我们实现 ImportBeanDefinitionRegistrar 中的方法,具体代码如下:
@Import(MyImportBeanDefinitionRegistrar.class)
public class Demo1 {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);
Person bean = applicationContext.getBean(Person.class);
System.out.println(bean);
}
}
class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
// 构建一个beanDefinition, 关于beanDefinition我后续会介绍,可以简单理解为bean的定义.
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Person.class).getBeanDefinition();
// 将beanDefinition注册到Ioc容器中.
registry.registerBeanDefinition("person", beanDefinition);
}
}
上述实现其实和Import的第二种方式差不多,都需要去实现接口,然后进行导入。接触到了一个新的概念,BeanDefinition,可以简单理解为bean的定义(bean的元数据),也是需要放在IOC容器中进行管理的,先有bean的元数据,applicationContext再根据bean的元数据去创建Bean。
@Import + DeferredImportSelector
这种方式也需要我们进行实现接口,其实它和@Import的第二种方式差不多,DeferredImportSelector 它是 ImportSelector 的子接口,所以实现的方法和第二种无异。只是Spring的处理方式不同,它和Spring Boot中的自动导入配置文件 延迟导入有关,非常重要。使用方式如下:
@Import(MyDeferredImportSelector.class)
public class Demo1 {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);
Person bean = applicationContext.getBean(Person.class);
System.out.println(bean);
}
}
class MyDeferredImportSelector implements DeferredImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
// 也是直接将Person的全限定名放进去
return new String[]{Person.class.getName()};
}
}
关于@Import注解的使用方式,大概就以上三种,当然它还可以搭配@Configuration注解使用,用于导入一个配置类。
使用FactoryBean接口
FactoryBean接口和BeanFactory千万不要弄混了,从名字其实可以大概的区分开,FactoryBean, 后缀为bean,那么它其实就是一个bean, BeanFactory,顾名思义 bean工厂,它是IOC容器的顶级接口,这俩接口其实都很重要。
代码示例:
@Configuration
public class Demo1 {
@Bean
public PersonFactoryBean personFactoryBean() {
return new PersonFactoryBean();
}
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);
Person bean = applicationContext.getBean(Person.class);
System.out.println(bean);
}
}
class PersonFactoryBean implements FactoryBean<Person> {
/**
* 直接new出来Person进行返回.
*/
@Override
public Person getObject() throws Exception {
return new Person();
}
/**
* 指定返回bean的类型.
*/
@Override
public Class<?> getObjectType() {
return Person.class;
}
}
上述代码,我使用@Configuration + @Bean的方式将 PersonFactoryBean 加入到容器中,注意,我没有向容器中注入 Person, 而是直接注入的 PersonFactoryBean 然后从容器中拿Person这个类型的bean,成功运行。
使用 BeanDefinitionRegistryPostProcessor
其实这种方式也是利用到了 BeanDefinitionRegistry,在Spring容器启动的时候会执行 BeanDefinitionRegistryPostProcessor 的 postProcessBeanDefinitionRegistry 方法,大概意思就是等beanDefinition加载完毕之后,对beanDefinition进行后置处理,可以在此进行调整IOC容器中的beanDefinition,从而干扰到后面进行初始化bean。
具体代码如下:
public class Demo1 {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
MyBeanDefinitionRegistryPostProcessor beanDefinitionRegistryPostProcessor = new MyBeanDefinitionRegistryPostProcessor();
applicationContext.addBeanFactoryPostProcessor(beanDefinitionRegistryPostProcessor);
applicationContext.refresh();
Person bean = applicationContext.getBean(Person.class);
System.out.println(bean);
}
}
class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Person.class).getBeanDefinition();
registry.registerBeanDefinition("person", beanDefinition);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}
上述代码中,我们手动向beanDefinitionRegistry中注册了person的BeanDefinition。最终成功将person加入到applicationContext中,上述的几种方式的具体原理,我后面会进行介绍。
小结
介绍了向spring容器中加入bean的几种方式.
@Configuration + @Bean
@ComponentScan + @Component
@Import 配合接口进行导入
使用FactoryBean。
实现BeanDefinitionRegistryPostProcessor进行后置处理。
其实向Spring中加入bean的方式有很多种,这里简要介绍了上面这几种。
来源:https://www.jianshu.com/p/5a531e6392d6


猜你喜欢
- 一、RabbitMQ介绍RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发
- 比如,我们有这么个过程,项目结构如下:a --b --ca是总结点,b是子节点,c是父节点b依赖父节点class,通
- 一、OutputStreamWriter流 API说明:OutputStreamWriter是从字符流到
- 此文通过一段代码来展示java获取相关参数的方法分享给大家:public static void main(String[] args) {
- 前言:前两天做项目的时候,想提高一下插入表的性能优化,因为是两张表,先插旧的表,紧接着插新的表,一万多条数据就有点慢了后面就想到
- 初步的想法是用两个recordset,一个从SQL取数据,一个往Access里面插入数据 因为表的字段比较多,所以只好用一个循环while
- 一、简介随着 Apple 发布 iPhone X 之后,各大手机厂商也开始模仿这种刘海屏的设计,而且刘海屏手机的用户也是越来越大,前段时间将
- 在Linux中创建一个新进程的唯一方法是使用fork()函数。fork()函数是Linux中一个非常重要的函数,和以往遇到的函数有一些区别,
- 先来说一说我们为什么要用这个东西啊!比如,我们现在有这样了个问题要解决:这样,我们就要用到中间消息间了然后我们就说一下什么是中间消息间吧。采
- 在编写程序,我们经常会对一些时间进行比较,比如要搜寻一个时间范围中的数据,需要用户输入开始时间和结束时间,如果结束时间小于或等于开始时间,那
- 开始研究android开发,搭建开发环境的时候就出了问题……果然是好事多磨~ 安装了jdk,配置环境变量,安装了完整版的adt、创建了hel
- 一、线性布局LinearLayout有两种排序方式orientation属性值为horizontal时,内部视图在水平方向从左往右排列。or
- 修订功能可以跟踪文档所有的修改,了解修改的过程,这对于团队协同文档编辑、审阅是非常有用的一个功能。将工作簿发送给他人审阅时,我们可以开启修订
- 背景环境已学习java基础,html,css,js,jquery,bootstrap,layui,maven,servlet和jsp,刚进入
- 最近做的一个项目涉及到文件上传与下载。前端上传采用百度webUploader插件。有关该插件的使用方法还在研究中,日后整理再记录。本文主要介
- 本文介绍为了实现高效并发,虚拟机对 synchronized 做的一系列的锁优化措施高效并发是从 JDK5 升级到 JDK6 后一项重要的改
- 一、问题分析及解决方案1、问题分析上一章我们讲过远程仓储统一管理配置信息,客户端可以通过统一配置服务中心 config server 服务端
- 本文实例总结了Android横竖屏切换相关技巧。分享给大家供大家参考,具体如下:一、禁止横竖屏切换Android横竖屏切换在手机开发中比较常
- 一、构造函数构造函数的最大作用就是创建对象时完成初始化,当我们在new一个对象并传入参数的时候,会自动调用构造函数并完成参数的初始化。如下:
- 使用 transient 修饰private transient String noColumn;使用 static 修饰private s