Spring IOC控制反转示例详解其Bean的声明周期
一.概述
今天给大家分享的技术是Spring IOC,IOC(Inversion of Control)控制反转,它是一种设计思想,是将我们手动创建对象及依赖代码交由IOC容器进行创建,管理。而DI即“依赖注入”和IOC其实是同一个概念的东西,只是从不同角度描述透析其本质。
下面分别做个简要阐述:
1. IOC控制反转:所谓的控制反转就是将代码里需要实现的对象的创建,管理以及对象的查找反转给IOC容器帮忙实现。从而达到对象与对象之间松耦合,可复用,可扩展;
2. DI 依赖注入:组件之间的依赖关系由动态容器主动注入,即在容器运行期间决定,将某个依赖关系注入到组件之中。依赖注入主要是为了提升组件重用的频率,提高系统的灵活性,可扩展性。
下面通过两张图对比一下,IOC到底干了那些事情,请看下图所示:
二.执行过程
下面先看看流程图,以便更深入的理解IOC的执行原理。流程图如下:
1.首先通过BeanDefinitionReader读取配置文件生成Bean的定义信息;
2. 然后可通过BeanFactoryPostProcessor自定义修改Bean的定义信息;(注:只需要实现BeanFactoryPostProcessor接口即可)如下所示:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
/**
*
*定义BeanFactoryPostProcessor的实现类,修改Bean的定义信息
*
**/
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
//DTO对BeanDefinition定义的信息进行修改
}
}
3.得到修改后BeanDefinition
的定义信息后,创建对象,进入Bean的生命周期,从对象实例化开始一直到销毁的整个生命过程;
三. Bean的生命周期
通过图3,大致描述了Bean的生命周期;其实详尽点的流程如下:
- 实例化前置(InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation());
- 对象实例化
- 实例化后置(InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation());
- 属性修改(InstantiationAwareBeanPostProcessor.postProcessPropertyValues());
- 属性填充(即:给属性赋值);
- 若实现了Aware接口,执行Aware接口的方法,给容器赋值;
- 初始化前置(BeanPostProcessor.postProcessBeforeInitialization());
- 执行初始化方法;同时调用它们的afterPropertiesSet接口方法,类似的如果bean使用了init-method属性声明了初始化方法,改方法也会被调用;
- 初始化后置(BeanPostProcessor.postProcessAfterInitialization());
- 此时的Bean已经被创建好了,可以被应用程序使用了,他们将一直驻留在应用上下文中,直到该应用上下文被销毁;
- 销毁;
四. 示例演示
1. 首先定义一个Person实体类;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
public class Person implements BeanFactoryAware, BeanNameAware,
InitializingBean, DisposableBean {
private String name;
private String address;
private int phone;
private BeanFactory beanFactory;
private String beanName;
public Person() {
System.out.println("执行步骤 6-->【构造器】调用Person的构造器实例化");
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("执行步骤 9-->【注入属性】注入属性name");
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
System.out.println("执行步骤 8-->【注入属性】注入属性address");
this.address = address;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
System.out.println("执行步骤 10-->【注入属性】注入属性phone");
this.phone = phone;
}
@Override
public String toString() {
return "Person [address=" + address + ", name=" + name + ", phone="
+ phone + "]";
}
// 这是BeanFactoryAware接口方法
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("执行步骤 12-->【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()");
this.beanFactory = arg0;
}
// 这是BeanNameAware接口方法
@Override
public void setBeanName(String arg0) {
System.out.println("执行步骤 11-->【BeanNameAware接口】调用BeanNameAware.setBeanName()");
this.beanName = arg0;
}
// 这是InitializingBean接口方法
@Override
public void afterPropertiesSet() throws Exception {
System.out
.println("执行步骤 14-->【InitializingBean接口】调用InitializingBean.afterPropertiesSet()");
}
// 这是DiposibleBean接口方法
@Override
public void destroy() throws Exception {
System.out.println("【DiposibleBean接口】调用DiposibleBean.destory()");
}
// 通过<bean>的init-method属性指定的初始化方法
public void myInit() {
System.out.println("执行步骤 15-->【init-method】调用<bean>的init-method属性指定的初始化方法");
}
// 通过<bean>的destroy-method属性指定的初始化方法
public void myDestory() {
System.out.println("执行步骤 35-->【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
}
}
2. 创建需要读取bean信息的配置信息;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<bean id="beanPostProcessor" class="com.grq.BeanLife.MyBeanPostProcessor">
</bean>
<bean id="instantiationAwareBeanPostProcessor" class="com.itcdns.MyInstantiationAwareBeanPostProcessor">
</bean>
<bean id="beanFactoryPostProcessor" class="com.itcdns.MyBeanFactoryPostProcessor">
</bean>
<bean id="person" class="com.itcdns.Person" init-method="myInit"
destroy-method="myDestory" scope="singleton" p:name="张三" p:address="广州"
p:phone="158449459481" />
</beans>
3. 实现BeanFactoryPostProcessor接口;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public MyBeanFactoryPostProcessor() {
super();
System.out.println("执行步骤 1-->这是BeanFactoryPostProcessor实现类构造器!!");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
throws BeansException {
System.out.println("执行步骤 2-->BeanFactoryPostProcessor调用postProcessBeanFactory方法");
BeanDefinition bd = arg0.getBeanDefinition("person");
bd.getPropertyValues().addPropertyValue("phone", "110");
}
}
4. 继承InstantiationAwareBeanPostProcessorAdapter ;(注:InstantiationAwareBeanPostProcessorAdapter实现SmartInstantiationAwareBeanPostProcessor接口,且SmartInstantiationAwareBeanPostProcessor继承InstantiationAwareBeanPostProcessor接口)
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import java.beans.PropertyDescriptor;
public class MyInstantiationAwareBeanPostProcessor extends
InstantiationAwareBeanPostProcessorAdapter {
public MyInstantiationAwareBeanPostProcessor() {
super();
System.out.println("执行步骤 4-->这是InstantiationAwareBeanPostProcessorAdapter实现类构造器!!");
}
// 接口方法、实例化Bean之前调用
@Override
public Object postProcessBeforeInstantiation(Class beanClass,
String beanName) throws BeansException {
System.out.println("执行步骤 5/18-->InstantiationAwareBeanPostProcessor调用postProcessBeforeInstantiation方法");
return null;
}
// 接口方法、实例化Bean之后调用
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("执行步骤 17/30-->InstantiationAwareBeanPostProcessor调用postProcessAfterInitialization方法");
return bean;
}
// 接口方法、设置某个属性时调用
@Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs,
PropertyDescriptor[] pds, Object bean, String beanName)
throws BeansException {
System.out.println("执行步骤 7/20-->InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法");
return pvs;
}
}
5. 实现BeanPostProcessor接口;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
public MyBeanPostProcessor() {
super();
System.out.println("执行步骤 3-->这是BeanPostProcessor实现类构造器!!");
// TODO Auto-generated constructor stub
}
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("执行步骤 16/29-->BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改!");
return arg0;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("执行步骤 13/26-->BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改!");
return arg0;
}
}
6. 测试,查看执行流程;
public static void main(String[] args) {
System.out.println("执行步骤 0-->现在开始初始化容器");
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
System.out.println("执行步骤 31-->容器初始化成功");
//得到Preson,并使用
Person person = context.getBean("person",Person.class);
System.out.println(person);
System.out.println("执行步骤 32-->现在开始关闭容器!");
((ClassPathXmlApplicationContext)context).registerShutdownHook();
}
执行结果如下图所示:
五. 结语
通过上面对spring ioc以及bean生命周期的讲解,希望对大家有所帮助!!!