网站首页 新闻首页 网页设计图形动画软件编程网站开发办公软件操作系统数据库网络技术认证考试范文资料黑客攻防 书籍教程 进入论坛

Bean的生命周期

http://www.diybl.com/ 2007-12-6  网络 点击:  [ 评论 ]
文章搜索:    【点击打包该文章】



一、了解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());
    }

}
 

欢迎光临DIY部落,点击这里查看更多文章教程   【点击打包该文章】
如果图片或页面不能正常显示请点击这里 站内搜索:   

文章评论

请您留言