简单比较init-method,afterPropertiesSet和BeanPostProcess

1、简单介绍java

一、init-method方法,初始化bean的时候执行,能够针对某个具体的bean进行配置。init-method须要在applicationContext.xml配置文档中bean的定义里头写明。例如:spring

<bean id="TestBean" class="nju.software.xkxt.util.TestBean" init-method="init"></bean>

这样,当TestBean在初始化的时候会执行TestBean中定义的init方法。服务器

 

二、afterPropertiesSet方法,初始化bean的时候执行,能够针对某个具体的bean进行配置。afterPropertiesSet 必须实现 InitializingBean接口。实现 InitializingBean接口必须实现afterPropertiesSet方法。app

三、BeanPostProcessor,针对全部Spring上下文中全部的bean,能够在配置文档applicationContext.xml中配置一个BeanPostProcessor,而后对全部的bean进行一个初始化以前和以后的代理。BeanPostProcessor接口中有两个方法: postProcessBeforeInitialization和postProcessAfterInitialization。 postProcessBeforeInitialization方法在bean初始化以前执行, postProcessAfterInitialization方法在bean初始化以后执行。ide

        总之,afterPropertiesSet 和init-method之间的执行顺序是afterPropertiesSet 先执行,init-method 后执行。从BeanPostProcessor的做用,能够看出最早执行的是postProcessBeforeInitialization,而后是afterPropertiesSet,而后是init-method,而后是postProcessAfterInitialization。post

 

2、相关用法及代码测试测试

一、PostProcessor类,实现BeanPostProcessor接口,实现接口中的postProcessBeforeInitialization,postProcessAfterInitialization方法this

package nju.software.xkxt.util;  

  

import org.springframework.beans.BeansException;  

import org.springframework.beans.factory.config.BeanPostProcessor;  

  

/** 

 * 定义Bean初始化先后的动做 

 *  

 * @author typ 

 *  

 */  

public class PostProcessor implements BeanPostProcessor {  

    @Override  

    public Object postProcessBeforeInitialization(Object bean, String beanName)  

            throws BeansException {  

        System.out.println("------------------------------");  

        System.out.println("对象" + beanName + "开始实例化");  

        return bean;  

    }  

  

    @Override  

    public Object postProcessAfterInitialization(Object bean, String beanName)  

            throws BeansException {  

        System.out.println("对象" + beanName + "实例化完成");  

        System.out.println("------------------------------");  

        return bean;  

    }  

  

}  

该PostProcessor类要做为bean定义到applicationContext.xml中,以下spa

<bean class="nju.software.xkxt.util.PostProcessor"></bean>

 

二、TestBean类,用作测试Bean,观察该Bean初始化过程当中上面4个方法执行的前后顺序和内容。实现InitializingBean接口,而且实现接口中的afterPropertiesSet方法。最后定义做为init-method的init方法。代理

package nju.software.xkxt.util;  
  
import org.springframework.beans.factory.InitializingBean;  
  
/** 
 * 用作测试Bean,观察该Bean初始化过程当中上面4个方法执行的前后顺序和内容 
 *  
 * @author typ 
 *  
 */  
public class TestBean implements InitializingBean {  
    String name;  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public void init() {  
        System.out.println("init-method is called");  
        System.out.println("******************************");  
    }  
  
    @Override  
    public void afterPropertiesSet() throws Exception {  
        System.out.println("******************************");  
        System.out.println("afterPropertiesSet is called");  
        System.out.println("******************************");  
    }  
}

启动Tomcat服务器,能够看到服务器启动过程当中,完成对Bean进行初始化。执行结果以下:

 

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

对象TestBean开始实例化

******************************

afterPropertiesSet is called

******************************

init-method is called

******************************

对象TestBean实例化完成

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

相关文章
相关标签/搜索