Bean的生命周期

一、了解bean的生命周期
只有singleton行为的bean接受容器管理生命周期。
non
- singleton行为的bean,Spring容器仅仅是new的替代,容器只负责创建。

二、定制bean的生命周期行为
1 .依赖关系注入之后的行为
public   class  Chinese  implements  Person  {
    
private Axe axe;
    
public Chinese() {
        System.out.println(
"Spring实例化主调bean:Chinese实例...");
    }

    
public void setAxe(Axe axe) {
        System.out.println(
"Spring执行依赖关系注入...");
    }

    
public void init() {
        System.out.println(
"正在执行初始化方法...");
    }

}

<? xml versin = " 1.0 "  encoding = " gb2312 " ?>
<! DOCTYPE beans PUBLIC " -//SPRING//DTD BEAN//EN "
    
" http://www.springframework.org/dtd/spring-beans.dtd " >
< beans >
    
< bean id = " steelAxe "   class = " prolove.SteelAxe " />
    
< bean id = " chinese "   class = " prolove.Chinese "  init - method = " init " >
        
< property name = " axe " >
            
< ref local = " steelAxe " />
        
</ property >
    
</ bean >
</ beans >

public   class  Chinese  implements  Person, InitializingBean  {
    
private Axe axe;
    
public Chinese() {
        System.out.println(
"Spring实例化主调bean:Chinese实例...");
    }

    
public void setAxe(Axe axe) {
        System.out.println(
"Spring执行依赖关系注入...");
    }

    
public void afterPropertiesSet() throws Exception {
        System.out.println(
"正在执行初始化方法...");
    }

}


2 .bean销毁之前的行为
public   class  Chinese  implements  Person  {
    
private Axe axe;
    
public Chinese() {
        System.out.println(
"Spring实例化主调bean:Chinese实例...");
    }

    
public void setAxe(Axe axe) {
        System.out.println(
"Spring执行依赖关系注入...");
    }

    
public void close() {
        System.out.println(
"正在执行销毁前的资源回收方法...");
    }

}

<? xml versin = " 1.0 "  encoding = " gb2312 " ?>
<! DOCTYPE beans PUBLIC " -//SPRING//DTD BEAN//EN "
    
" http://www.springframework.org/dtd/spring-beans.dtd " >
< beans >
    
< bean id = " steelAxe "   class = " prolove.SteelAxe " />
    
< bean id = " chinese "   class = " prolove.Chinese "  destroy - method = " init " >
        
< property name = " axe " >
            
< ref local = " steelAxe " />
        
</ property >
    
</ bean >
</ beans >

public   class  Chinese  implements  Person, DisposableBean  {
    
private Axe axe;
    
public Chinese() {
        System.out.println(
"Spring实例化主调bean:Chinese实例...");
    }

    
public void setAxe(Axe axe) {
        System.out.println(
"Spring执行依赖关系注入...");
    }

    
public void destroy() throws Exception {
        System.out.println(
"正在执行销毁前的资源回收方法...");
    }

}


三、协调不同步的bean
public   class  SteelAxe  implements  Axe  {
    
private int count = 0;
    
public SteelAxe() {
        System.out.println(
"Spring实例化依赖bean:SteelAxe实例");
    }

    
public String chop() {
        
return "钢斧砍柴真快" + ++count;
    }

}


public   abstract   class  Chinese  implements  Person  {
    
private Axe axe;
    
public Chinese() {
        System.out.println(
"Spring实例化主调bean:Chinese实例...");
    }

    
//方法注入所需要的方法,该方法由Spring提供实现
    public abstract Axe createAxe();
    
    
public void setAxe(Axe axe) {
        System.out.println(
"Spring执行依赖关系注入...");
        
this.axe = axe;
    }

    
public Axe getAxe() {
        
return axe;
    }

}


<? xml versin = " 1.0 "  encoding = " gb2312 " ?>
<! DOCTYPE beans PUBLIC " -//SPRING//DTD BEAN//EN "
    
" http://www.springframework.org/dtd/spring-beans.dtd " >
< beans >
    
< bean id = " steelAxe "   class = " prolove.SteelAxe "  singleton = " false " />
    
< bean id = " chinese "   class = " prolove.Chinese " >
        
< lookup - method name = " createAxe "  bean = " steelAxe " />
        
< property name = " axe " >
            
< ref local = " steelAxe " />
        
</ property >
    
</ bean >
</ beans >

public   class  BeanTest  {
    
public static void main(String[] args) throws Exception {
        InputStream is 
= new FileInputStream("bean.xml");
        InputStreamResource isr 
= new InputStreamResource(is);
        XmlBeanFactory factory 
= new XmlBeanFactory(isr);
        Person p 
= (Person)factory.getBean("chinese");

        Axe axe1 
= p.getAxe();
        Axe axe2 
= p.getAxe();
        System.out.println(
"没有采用Lookup方法注入:");
        System.out.println(
"Random的两个实例指向同一个引用:" + (axe1 == axe2));
        System.out.println(axe1.chop());
        System.out.println(axe2.chop());
        
//调用Lookup注入,则保证每次产生新的nog-singleton bean实例
        Axe axe3 = p.createAxe();
        Axe axe4 
= p.createAxe();
        System.out.println(
"采用Lookup方法注入之后:");
        System.out.println(
"Random的两个实例指向同一个引用:" + (axe1 == axe2));
        System.out.println(axe3.chop());
        System.out.println(axe4.chop());
    }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值