在spring 框架中 Spring Ioc容器对bean进行管理,负责bean的整个生命周期。具体的过程如下图所示:
参考文献:https://www.cnblogs.com/kenshinobiy/p/4652008.html
1、实例化bean
2、属性注入
3、BeanNameAware的setBeanName(String beanName)
实现了BeanNameAware接口的bean会调用setBeanName(String beanName)方法将bean的id值注入给bean的某个属性,获取到自身在Spring容器中的id属性
public class User implements BeanNameAware{
private String id;
private String name;
private String address;
public void setBeanName(String beanName) {
//ID保存BeanName的值
id=beanName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
bean的配置如下:
<bean id="zhangsan" class="com.github.jettyrun.springinterface.demo.aware.beannameaware.User">
<property name="name" value="zhangsan"></property>
<property name="address" value="火星"></property>
</bean>
测试BeanNameAware接口作用:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application-beanaware.xml");
User user=context.getBean(User.class);
System.out.println(String.format("实现了BeanNameAware接口的信息BeanId=%s,所有信息=%s",user.getId(),user.toString()));
}
运行结果:
实现了BeanNameAware接口的信息BeanId=zhangsan,所有信息=User{id='zhangsan', name='zhangsan', address='火星'}
参考文献:https://www.jianshu.com/p/c5c61c31080b
4、BeanFactoryAware的setBeanFactory(BeanFactory beanFactory)
实现 BeanFactoηAware接口的bean可以直接访问Spring容器,通过BeanFactory来获取spring容器中的其他bean
@Service
public class BeanFactoryHelper implements BeanFactoryAware {
private static BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public static Object getBean(String beanName){
if(beanFactory == null){
throw new NullPointerException("BeanFactory is null!");
}
return beanFactory.getBean(beanName);
}
}
参考文献:https://www.cnblogs.com/handsomeye/p/6277510.html
5、ApplicationContextAware的setApplicationContext(ApplicationContext)
如果这个Bean已经实现了ApplicationContextAware接口,会调用setApplicationContext(ApplicationContext applicationContext)方法,传入Spring上下文(同样这个方式也可以实现步骤4的内容,但比4更好,因为ApplicationContext是BeanFactory的子接口,有更多的实现方法)
@Service
public class ApplicationContextHelper implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public static Object getBean(String beanName){
if(applicationContext == null){
throw new NullPointerException("ApplicationContext is null!");
}
return applicationContext.getBean(beanName);
}
}
6、BeanPostProcessors的ProcessBeforeInitialization(Object bean,String beanName)
BeanPostProcessors接口是bean的加工器,应该是在bean的实例化过程中对bean做一些包装处理包括前置处理和后置处理,可以对bean中的属性值进行修改
public class PostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean,
String beanName) throws BeansException {
对bean进行处理
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean,
String beanName) throws BeansException {
对bean进行处理
return bean;
}
}
7、initializingBean的afterPropertiesSet()
实现了initializingBean接口的bean可以调用afterPropertiesSet()方法对bean进行初始化配置
8、自定义init-method
声明了init-method的bean会调其对应的自定义初始化方法
9、BeanPostProcessors的ProcessaAfterInitialization(Object bean,String beanName)
初始化之后的后置处理
10、DisposableBean的destroy()
在容器关闭时,如果Bean类有实现org.springframework.beans.factory.DisposableBean接口,则执行他的destroy()方法
11、Bean定义文件中定义destroy-method
在容器关闭时,可以在Bean定义文件中使用"destroy-method"属性设定方法名称
如果应用Spring的工厂也就是BeanFactory的话去掉第5步就Ok了。