您的位置:首页 > 编程语言 > Java开发

Spring Bean的生命周期

2019-07-03 21:07 1251 查看

 

Spring容器可以管理singleton作用域的Bean的生命周期,可以调用创建、初始化、销毁等生命周期的方法。

对于prototype作用域的Bean,Spring容器只负责创建,创建后Bean的实例就交给客户端代码来管理,Spring容器不再跟踪其生命周期。

 

 

Bean的生命周期

1、根据配置情况,调用Bean的构造方法或工厂方法实例化Bean

2、利用依赖注入完成Bean中所有属性值的配置注入

 

3、如果Bean实现了BeanNameAware接口,则调用setBeanName()方法传入当前Bean的id。

4、如果Bean实现了BeanFactoryAware接口,则调用setBeanFactory()传入当前工厂实例的引用

5、如果Bean实现了ApplicationContextAware接口,则调用setApplicationContext()方法传入当前ApplicationContext实例的引用

 

6、如果BeanPostProcessor和Bean关联,则调用预初始化方法postProcessBeforeInitialzation()进行加工操作,Spring AOP即利用此实现。

 

7、如果Bean实现了InitializingBean接口,则调用afterPropertiesSet()方法

8、如果在配置文件中通过init-method属性指定了初始化方法,则调用初始化方法

 

9、如果BeanPostProcessor和Bean关联,则调用初始化方法postProcessAfterInitialization()。此时,Bean已经可以被正常使用了。

 

10、如果指定了作用域为singleton,则将实例放在Spring IoC的缓存池中,并触发Spring容器对该Bean的生命周期管理,如果指定作用域为prototype,则将该Bean交给调用者,由调用者管理该Bean的生命周期。

 

11、如果Bean实现了DisposableBean接口,则调用destory()方法销毁实例;

12、如果在配置文件中通过destory-method指定了Bean的销毁方法,则调用该方法销毁实例。

 

 

说明:

以上接口中,均只有一个方法。

并不建议让Bean实现多个接口,因为继承多个接口会使代码耦合较高。

 

 

 

 

注入依赖后的行为

Spring提供2种方式,在Bean全部属性设置完成后执行特定的行为:

  • 实现InitializingBean接口,在afterPropertiesSet()方法中写代码。InitializingBean接口中只有这一个方法。
  • 在xml中使用init-method属性指定要调用的方法

可以同时使用这2种方式,会先执行afterPropertiesSet(),再执行init-method属性指定的方法。

 

 

 

Bean销毁之前的行为

Spring提供了2种方式,在Bean销毁之前执行特定的行为:

  • 实现DisposableBean接口,该接口只有destory()方法
  • 在xml中用destory-method属性指定要调用的方法

可以同时使用这2种方式,会先执行destory(),再执行destory-method属性指定的方法。

 

说明:同时使用时,都是先执行接口中的方法,再执行xml中指定的方法。

 

 

 

示例

class Student {
private String name;

public Student(String name){
this.name=name;
}

public String getName(){
return name;
}
}

class Teacher implements InitializingBean, DisposableBean {
@Autowired
private Student student;

public void say(){
System.out.println(student.getName()+",叫家长来一下");
}

public void xmlInit(){
System.out.println("正在执行xml中init-method属性指定的初始化方法");
}

@Override
public void afterPropertiesSet() throws Exception {
System.out.println("正在执行InitializingBean接口中的afterPropertiesSet()方法");
}

public void xmlDestory(){
System.out.println("正在执行xml中destory-method属性指定的方法");
}

@Override
public void destroy() throws Exception {
System.out.println("正在执行DisposableBean接口中的destory()方法");
}
}

public class Test {
public static void main(String[] args) {
AbstractApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Teacher teacher=applicationContext.getBean("teacher",Teacher.class);
teacher.say();
applicationContext.registerShutdownHook();
}

}

在基于Web的Spring容器中,系统已经提供了相应的代码,在Web应用关闭时,会自动关闭Spring容器。

在非Web的Spring的容器中,需要手动调用AbstractApplicationContext类的registerShutdownHook()方法向JVM注册一个关闭钩子,执行完前面的代码,会自动关闭Spring容器。

ApplicatioContext即Spring容器。

ApplicationContext是接口,没有实现registerShutdownHook()方法,AbstractApplicationContext是ApplicationContext的子类,实现了该方法,此处要声明为AbstractApplicationContext,不能声明为ApplicationContext。

 

 

xml中的配置:

<context:annotation-config />

<bean id="student" class="my_package.Student">
<constructor-arg value="张三" />
</bean>

<bean id="teacher" class="my_package.Teacher" init-method="xmlInit" destroy-method="xmlDestory" />

 

 

运行程序,控制台输出如下:

正在执行InitializingBean接口中的afterPropertiesSet()方法
正在执行xml中init-method属性指定的初始化方法
张三,叫家长来一下
正在执行DisposableBean接口中的destory()方法
正在执行xml中destory-method属性指定的方法

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: