Spring Beans的生命周期:
1. 实例化:
默认为调用该Bean的无参构造。 如果该Bean存在父类,则首先调用父类的无参构造。
2. 设置属性值:
3. 如果Bean实现了BeanNameAware接口,调用setBeanName方法。
4. 如果Bean实现了BeanFactoryAware接口,调用setBeanFactory方法。
5. 如果Bean实现了InitializingBean接口,调用afterPropertiesSet方法。
6. 如果配置文件(beans.xml)里设置了init-Method方法(对应的annotation为PostConstruct), 将会执行配置的init-Method方法
7. 如果Bean的scope为singleton(默认),容器将会把该bean放入Spring Ioc的缓存池里,并将Bean的引用返回给调用者,Spring容器将继续管理这个bean,如果bean实现了DisposableBean,将会调用destroy方法。如果配置文件定义了destroy-method,将会调用该方法。
8. 如果Bean的scope为prototype, 容器将Bean返回给调用者,以后都将由调用者负责Bean的生命周期。spring不再管理。 prototype和singleton的区别在于:singleton的bean的上述周期都是在容器初始化的时候执行的。 而prototype是在getBean的时候执行的。
ps: AbstractApplicationContext 含有销毁容易的方法。。registerShutdownHook
package inter;
public class Phone {
protected int price;
protected String name;
public Phone(int price,String name){
System.out.println("parent param construct");
this.price = price;
this.name = name;
}
public Phone(){
System.out.println("parent default construct");
}
public void speak(){
System.out.println(name+" speaks");
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
System.out.println("parent setPrice");
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("parent setName");
this.name = name;
}
}
package Impl;
import inter.Phone;
import javax.annotation.PostConstruct;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class IPhone extends Phone implements BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean{
public IPhone(int price, String name) {
super(price, name);
System.out.println("param construct");
}
public IPhone() {
System.out.println("default construct");
}
@PostConstruct
public void initMethod(){
System.out.println("init-Method");
}
@Override
public void speak(){
System.out.println(this.getName()+" general speaks ");
}
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("setBeanFactory");
}
@Override
public void setBeanName(String arg0) {
System.out.println("bean name:"+arg0);
}
@Override
public void setPrice(int price) {
// TODO Auto-generated method stub
this.price = price;
System.out.println("son Set price");
}
@Override
public void setName(String name) {
this.name = name;
System.out.println("son Set name");
}
@Override
public void destroy() throws Exception {
System.out.println("destory");
}
public void testDestroy(){
System.out.println("destroy-method");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet");
}
}
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="iphone" name="iphone2" class="Impl.IPhone" p:name="iphone4s" p:price="100" scope="singleton" destroy-method="testDestroy"/>
</beans>
分享到:
相关推荐
Spring bean生命周期demo
spring bean 的生命周期,把运行结果的日志,用sublime打开对比查看,你会有比较清晰的认识
Springbean生命周期
Spring Bean 生命周期之“我从哪里来?”
Spring Bean 的生命周期在整个 Spring 中占有很重要的位置,掌握这些可以加深对 Spring 的理解。这篇文章主要介绍了Spring Bean 生命周期,需要的朋友可以参考下
虽然可以随意配置 <bean> 的属性,但是建议不要过多地使 Bean 实现接,因为这样会导致代码和 Spring 的聚合过于紧密第 1 步:实例化Bean第
SpringBean的生命周期.mdj
本篇文章主要介绍了浅谈Spring bean 生命周期验证,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
这个工程主要实现了: Spring中Bean的生命周期 applicationcontext的应用(实现国际化,事件的传递)
主要介绍了Spring bean生命周期配置过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
此资源是我的博客bean的生命周期的测试代码,只有源代码,没有相关库文件,环境是spring4.2 ,
主要介绍了Spring Bean的生命周期的相关资料,需要的朋友可以参考下
本文主要介绍 Spring IoC 容器如何管理 Bean 的生命周期。 在应用开发中,常常需要执行一些特定的初始化工作,这些工作都是相对比较固定的,比如建立数据库连接,打开网络连接等,同时,在结束服务时,也有一些相对...
主要给大家介绍了Spring中Bean的生命周期和作用域及实现方式的相关资料,文中介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
介绍了Spring的Bean周期,容器周期,工厂周期,运行代码后可以看到运行结果
本篇文章主要介绍了spring之Bean的生命周期详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本篇文章主要介绍了深入理解Spring中bean的生命周期介绍,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring的生命周期是指实例化Bean时所经历的一系列阶段,即通过getBean()获取bean对象及设置对象属性时,Spring框架做了哪些事。Bean的生命周期从Spring容器实例化Bean到销毁Bean。 本文分别对 BeanFactory 和 ...
学习Spring过程中,使用Eclipse调试Spring源码的关键断点文件。