java创建bean并注册到spring中

本文介绍了Spring 3.0引入的Java Code配置Bean Definition的方式,对比XML和Annotation配置的区别。详细阐述了BeanDefinitionRegistryPostProcessor的作用,以及如何通过Java代码创建并注册Bean到Spring容器。还提到了Spring的ApplicationContextAware、BeanDefinitionRegistryPostProcessor和ApplicationListener三个关键Hook类的调用顺序。文章以创建接口及其实现类为例,展示了如何实现MyBeanDefinitionRegistryPostProcessor类来注册Bean,并提供了测试代码。
摘要由CSDN通过智能技术生成

从Spring 3.0开始,增加了一种新的途径来配置Bean Definition,这就是通过Java Code配置Bean Definition。


       与XML和Annotation两种配置方式不同点在于:

    前两种方式XML和Annotation的配置方式为预定义方式,即开发人员通过XML文件或者Annotation预定义配置Bean的各种属性后,启动Spring容器,Spring容器会首先解析这些配置属性,生成对应的Bean Definition,装入到DefaultListtableBeanFactory对象的属性容器中,以此同时,Spring框架也会定义内部使用的Bean定义,如Bean名为:org.springframework.context.annotation.internalConfigurationAnnotationProcessor”的 ConfigurationClassPostProcessor 定义。而后此刻不会做任何Bean Definition的解析动作,Spring框架会根据前两种配置,过滤出BeanDefinitionRegistryPostProcessor 类型的Bean定义,并通过Spring框架生成对应的Bean对象(如 ConfigurationClassPostProcessor 实例)。。结合 Spring 上下文源码可知这个对象是一个 processor 类型工具类,Spring 容器会在实例化开发人员所定义的 Bean 前先调用该 processor 的 postProcessBeanDefinitionRegistry(…) 方法。此处实现基于 Java Code 配置Bean Definition的处理。


     基于 Java Code 的配置方式,其执行原理不同于前两种。它是在 Spring 框架已经解析了基于 XML 和 Annotation 配置后,通过加入 BeanDefinitionRegistryPostProcessor 类型的 processor 来处理配置信息,让开发人员通过 Java 编程方式定义一个 Java 对象。其优点在于可以将配置信息集中在一定数量的 Java 对象中,同时通过 Java 编程方式,比基于 Annotation 方式具有更高的灵活性。并且该配置方式给开发人员提供了一种非常好的范例来增加用户自定义的解析工具类。其主要缺点在于与 Java 代码结合紧密,配置信息的改变需要重新编译 Java 代码,另外这是一种新引入的解析方式,需要一定的学习成本。


提及一点的就是,Spring框架有3个主要的Hook类,分别是:


org.springframework.context.ApplicationContextAware 

它的setApplicationContext 方法将在Spring启动之前第一个被调用。我们用来同时启动Jdon框架。


org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor 

它的postProcessBeanDefinitionRegistry 和 postProcessBeanFactory 方法是第二和第三被调用,它们在Bean初始化创建之前启动,如果Spring的bean需要的其他第三方中的组件,我们在这里将其注入给Spring。


org.springframework.context.ApplicationListener 

用于在初始化完成后做一些事情,当Spring所有XML或元注解的Bean都启动被创建成功了,这时会调用它的唯一方法onApplicationEvent。

 

下面我们来完成一个,自己通过java代码创建bean,并注册为Spring管理。 

<!--[if !supportLineBreakNewLine]-->

<!--[endif]-->


本例中,我们创建一个接口,然后创建该接口的2个实现类,分别命名不同的名字,然后在需要注入的地方使用@Qualifier 指定注入对应的实例。

接口com.kfit.demo.Shanhy.java

1
2
3
4
5
6
7
package  com.kfit.demo;
  
publicinterface Shanhy {
   
    publicvoid dispaly();
   
}

实现类com.kfit.demo.ShanhyA.java

1
2
3
4
5
6
7
8
9
package  com.kfit.demo;
  
publicclass ShanhyA  implements  Shanhy{
  
    @Override
    publicvoid dispaly() {
       System.out.println( "ShanhyA.dispaly()" );
    }
}

实现类com.kfit.ShanhyB.java

1
2
3
4
5
6
7
8
9
10
package  com.kfit.demo;
  
publicclass ShanhyB  implements  Shanhy {
  
    @Override
    publicvoid dispaly() {
       System.out.println( "ShanhyB.dispaly()" );
    }
  
}

定义接口BeanDefinitionRegistryPostProcessor的实现:

com.kfit.config.MyBeanDefinitionRegistryPostProcessor:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值