简单比较init-method,afterPropertiesSet和BeanPostProcess
一、简单介绍 1、init-method方法,初始化bean的时候执行,可以针对某个具体的bean进行配置。init-method需要在applicationContext.xml配置文档中bean的定义里头写明。例如: <bean id="TestBean" class="nju.software.xkxt.util.TestBean" init-method="init"></bean> 这样,当TestBean在初始化的时候会执行TestBean中定义的init方法。 2、afterPropertiesSet方法,初始化bean的时候执行,可以针对某个具体的bean进行配置。afterPropertiesSet 必须实现 InitializingBean接口 。实现 InitializingBean接口必须实现afterPropertiesSet方法。 3、 BeanPostProcessor,针对所有Spring上下文中所有的bean ,可以在配置文档applicationContext.xml中配置一个BeanPostProcessor,然后对所有的bean进行一个初始化之前和之后的代理。BeanPostProcessor接口中有两个方法: postProcessBeforeInitialization和postProcessAfterInitialization。