bean装配过程

问题来源:

有一个bean为A,一个bean为B。想要A在容器实例化的时候的一个属性name赋值为B的一个方法funB的返回值。

如果只是在A里单纯的写着:

private B b;
private String name = b.funb();

会报错说nullpointException,因为这个时候b还没被set进来,所以为null。

解决办法为如下代码,同时学习下spring中InitializingBean对象构造方法init-method的执行顺序。

Java代码 复制代码 收藏代码
  1. public class Aimplements InitializingBean { 
  2.  
  3. private B b; 
  4. private String name; // = b.funb(); 
  5.  
  6. public void setB(B b) { 
  7.   System.out.println("A.setB initialed"); 
  8.   this.b = b; 
  9.  
  10. public A() { 
  11.   System.out.println("A initialed"); 
  12.  
  13. public void init() { 
  14.   System.out.println("init"); 
  15.   this.name = b.funb(); 
  16.  
  17. @Override 
  18. public String toString() {
  19.   return super.toString() +this.name; 
  20.  
  21. public void afterPropertiesSet()throws Exception { 
  22.  
  23.   //其实放在这里也可以 
  24.  
  25.   //this.name = b.funb(); 
  26.   System.out.println("afterPropertiesSet"); 
  27.  
  28.  
  29.  
  30. public class B { 
  31.  
  32. public String funb() { 
  33.   System.out.println("funb"); 
  34.   return "B.funb"
  35.  
  36. public B() { 
  37.   System.out.println("B initialed"); 

spring配置文件

<beans default-autowire="byName">
      <bean id="a" class="testspring.A" init-method="init">
      </bean>
      <bean id="b" class="testspring.B">
      </bean>
</beans>

测试代码:

public static void main(String[] args) {
      ApplicationContext context = new FileSystemXmlApplicationContext(
          "src/testspring/bean.xml");
       A a = (A) context.getBean("a");
      System.out.println(a);

}

程序输出为:

A initialed
B initialed
A.setB initialed
afterPropertiesSet
init
funb
testspring.A@50d89cB.funb

从这里看到A的name属性在bean加载完成的时候也被成功设置为B的funB方法的返回值了,要点就是用init-method来实现。

加载顺序也可以看到为:

先构造函数(按照默认规则:先实例化A,再实例化B)——>然后是b的set方法注入(即依赖注入)——>InitializingBean的afterPropertiesSet方法——>init-method方法

 

总结为:

以下内容是从书中摘录来的,但是我发现即使摘录一遍,对其内容的理解也会更加深入!
一、Spring装配Bean的过程
1. 实例化;
2. 设置属性值;
3. 如果实现了BeanNameAware接口,调用setBeanName设置Bean的ID或者Name;
4. 如果实现BeanFactoryAware接口,调用setBeanFactory 设置BeanFactory;
5. 如果实现ApplicationContextAware,调用setApplicationContext设置ApplicationContext
6. 调用BeanPostProcessor的预先初始化方法;
7. 调用InitializingBean的afterPropertiesSet()方法;
8. 调用定制init-method方法;
9. 调用BeanPostProcessor的后初始化方法;


Spring容器关闭过程
1. 调用DisposableBean的destroy();
2. 调用定制的destroy-method方法;

----------------------------------------------------------------------

------------------------------------------

使用构造函数的注入方式new A(B b)也可以

new A(B b)包含了A depends-on B的所有情况。既然已经定义了new A(B b)就没有必要在定义A depends-on B。所以,new A(B b)可以替代A depends-on B在A创建前必须创建B,而且A不需要使用B实例的情况下只能使用A depends-on B

 

 

---------------------------------------------------------------------------

----------------------------------------------

(1)在spring初始化bean的时候,如果bean实现了InitializingBean接口,会自动调用afterPropertiesSet方法。

(2)在spring初始化bean的时候,如果该bean是实现了InitializingBean接口,并且同时在配置文件中指定了init-method,系统则是先调用afterPropertiesSet方法,然后在调用init-method中指定的方法。

这方式在spring中是怎么实现的?

通过查看spring的加载bean的源码类(AbstractAutowireCapableBeanFactory)可看出其中奥妙
AbstractAutowireCapableBeanFactory类中的invokeInitMethods讲解的非常清楚,源码如下:
protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd)
            throws Throwable {
//判断该bean是否实现了实现了InitializingBean接口,如果实现了InitializingBean接口,则只掉调用bean的afterPropertiesSet方法
        boolean isInitializingBean = (bean instanceof InitializingBean);
        if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
            if (logger.isDebugEnabled()) {
                logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
            }
           
            if (System.getSecurityManager() != null) {
                try {
                    AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                        public Object run() throws Exception {
                           //直接调用afterPropertiesSet
                            ((InitializingBean) bean).afterPropertiesSet();
                            return null;
                        }
                    },getAccessControlContext());
                } catch (PrivilegedActionException pae) {
                    throw pae.getException();
                }
            }               
            else {
               //直接调用afterPropertiesSet
                ((InitializingBean) bean).afterPropertiesSet();
            }
        }
        if (mbd != null) {
            String initMethodName = mbd.getInitMethodName();
          //判断是否指定了init-method方法,如果指定了init-method方法,则再调用制定的init-method
            if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
                    !mbd.isExternallyManagedInitMethod(initMethodName)) {
                   //进一步查看该方法的源码,可以发现init-method方法中指定的方法是通过反射实现
                invokeCustomInitMethod(beanName, bean, mbd);
            }
        }
    }
总结:
1:spring为bean提供了两种初始化bean的方式,实现InitializingBean接口,实现afterPropertiesSet方法,或者在配置文件中同过init-method指定,两种方式可以同时使用
2:实现InitializingBean接口是直接调用afterPropertiesSet方法,比通过反射调用init-method指定的方法效率相对来说要高点。但是init-method方式消除了对spring的依赖
3:如果调用afterPropertiesSet方法时出错,则不调用init-method指定的方法。
4:TransactionTemplate实现InitializingBean接口,主要是判断transactionManager是否已经初始化,如果没有则抛出异常。源码如下:
     public void afterPropertiesSet() {
        if (this.transactionManager == null) {
            throw new IllegalArgumentException("Property 'transactionManager' is required");
        }
    }

5:Spirng的InitializingBean为bean提供了定义初始化方法的方式。InitializingBean是一个接口,它仅仅包含一个方法:afterPropertiesSet()(这就是那个初始化方法)。

     Spring在设置完一个bean所有的合作者后,会检查bean是否实现了InitializingBean接口,如果实现就调用bean的afterPropertiesSet方法。
     

Spring要求init-method是一个无参数的方法,如果init-method指定的方法中有参数,那么Spring将会抛出 java.lang.NoSuchMethodException
init-method指定的方法可以是public、protected以及private的,并且方法也可以是final的。
init-method指定的方法可以是声明为抛出异常的,就像这样:
       final protected void init() throws Exception{
           System.out.println("init method...");
           if(true) throw new Exception("init exception");
    }
如果在init-method方法中抛出了异常,那么Spring将中止这个Bean的后续处理,并且抛出一个 org.springframework.beans.factory.BeanCreationException异常。

 

如果一个bean被定义为非单例的,那么afterPropertiesSet和init-method在bean的每一个实例被创建时都会执行。单例 bean的afterPropertiesSet和init-method只在bean第一次被实例时调用一次。大多数情况下 afterPropertiesSet和init-method都应用在单例的bean上。 

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------

----------------------------------------------

spring初始化bean的顺序:

spring默认有个规则,总是先初始化主调bean,然后再初始化依赖bean。(此时主调bean内可能只是维持了一个依赖bean的引用,但此时还不能使用依赖bean,否则会出现nullpointerexception。)之后再将依赖bean注入给主调bean的引用,最终将具备完整依赖关系的bean返回给程序。

如果bean之间的依赖关系非常直接,spring容器在返回bean实例之前,会完成bean依赖关系的注入。假如bean A依赖于bean B,程序请求bean A时Spring会自动先初始化bean B再将B注入A,最后将具备完整依赖的A返回给程序。

但是,如果在bean间的依赖关系不够直接的情况下,比如下面代码,spring会按照上述默认规则办事。如果A的初始化依赖于B,但这样配置此时B还没有生成,这样会引发异常。

Java代码 复制代码 收藏代码
  1. <bean id="A" class="ExampleBean">
  2. <property name="B" ref="B" />
  3. </bean> 
  4. <bean id="B" class="ManagerBean" />

<beans default-autowire="byName">
      <bean id="a" class="testspring.A" init-method="init">
      </bean>
      <bean id="b" class="testspring.B">
      </bean>
</beans>

设置init-method或实现InitializingBean,都不会改变spring的默认规则,被依赖类还是在主调类实例化之后才实例化。)

等。

所以需要使用depends-on,它会在初始化主调bean前,强制初始化被依赖bean。

Java代码 复制代码 收藏代码
  1. <bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">
  2. <property name="manager" ref="manager" />
  3. </bean> 
  4. <bean id="manager" class="ManagerBean" />
  5. <bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />

 depends-on只是先实例化被依赖bean.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值