一、简单介绍
1、init-method方法,初始化bean的时候执行,可以针对某个具体的bean进行配置。init-method需要在applicationContext.xml配置文档中bean的定义里头写明。例如:<beanid="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。postProcessBeforeInitialization方法在bean初始化之前执行,postProcessAfterInitialization方法在bean初始化之后执行。
总之,afterPropertiesSet和init-method之间的执行顺序是afterPropertiesSet 先执行,init-method 后执行。从BeanPostProcessor的作用,可以看出最先执行的是postProcessBeforeInitialization,然后是afterPropertiesSet,然后是init-method,然后是postProcessAfterInitialization。
二、相关用法及代码测试
1、PostProcessor类,实现BeanPostProcessor接口,实现接口中的postProcessBeforeInitialization,postProcessAfterInitialization方法
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中,如下
<beanclass="nju.software.xkxt.util.PostProcessor"></bean>
2、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开始实例化
******************************
afterPropertiesSetiscalled
******************************
init-methodiscalled
******************************
对象TestBean实例化完成
------------------------------
分享到:
相关推荐
3、通过spring的xml bean配置或bean注解指定初始化方法,如下面实例的initMethod方法通过@bean注解指定。 销毁的时候实现的方法 1、通过java提供的@PreDestroy注释; 2、通过实现spring提供的DisposableBean接口,并...
在数据库中创建steam库,然后导入sql文件,修改相应的的配置,关于主从配置请自行百度啦,运行起项目,然后需要把`GameService`类以及`CommentService`类里面的`afterPropertiesSet`方法里面的代码注释掉,这是第一...
顺序:@PostConstruct> InitializingBean#afterPropertiesSet方法> initMethod Bean的预设初始化 延迟初始化和非延迟对象的差异:应用之上启动前后BeanInitialDemo Bean的销毁@PreDestroy> Dispo
【资源说明】 1、基于SpringBoot的高并发选课系统源码+项目说明(毕设).zip 2、该资源包括项目的全部... public void afterPropertiesSet() throws Exception { //预加载相关操作 } } ``` 通过上述代码所示,Spring
在数据库中创建steam库,然后导入sql文件,修改相应的的配置,关于主从配置请自行百度啦,运行起项目,然后需要把`GameService`类以及`CommentService`类里面的`afterPropertiesSet`方法里面的代码注释掉,这是第一...
bean),即使用普通的“new”运算符创建 bean,然后 spring 进一步控制初始化 bean,替换 @Value 占位符 bean 属性上的值,调用 afterPropertiesSet InitializingBean 类型,注入@Autowired 或@Inject 属性等。...
leke-qqwry leke-qqwry 是一个纯真IP地址库(qqwry.dat...fb.afterPropertiesSet(); IPv4Locator locator = fb.getObject(); IPv4Loc loc = locator.locate("39.184.174.243"); System.out.println(loc); // 浙江省 移动
.1. netty-sekiro 原文 感谢作者。以下是对原作者内容的学习提取 ...服务器端是用Springboot管理各种组件,在Springboot bean(InitializingBean)初始化 afterPropertiesSet完成会启动两个nio服务,分别监听5600
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() * 创建日期:2012-12-19 * 修改说明: * @author wangk */ @Override public void afterPropertiesSet() throws ...