惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
D
DataBreaches.Net
罗磊的独立博客
博客园 - 司徒正美
Last Week in AI
Last Week in AI
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
宝玉的分享
宝玉的分享
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
小众软件
小众软件
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
B
Blog
博客园 - 【当耐特】
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - xiaolei1982

asp.net的ajax的实现 在myeclipse下整合spring和hibernate(转贴) Struts+Spring+Hibernate练习(转贴) spring:bean注入(转贴) - xiaolei1982 - 博客园 解析eclipse下生成Hibernate DAO中的几个方法(转贴) ASP.NET ViewState 实现分析(转贴) 自定义控件源码(转贴) ASP.NET服务器控件的开发(4)(转贴) ASP.NET服务器控件的开发(3)(转贴) ASP.NET服务器控件的开发(2) (转贴) ASP.NET服务器控件的开发(1)(转贴) Web 页本是无状态而断续的(转贴) asp.net控件开发基础(3)(转贴) asp.net控件开发基础(6)(转贴) asp.net控件开发基础(5)(转贴) asp.net控件开发基础(4)(转贴) asp.net控件开发基础(2)(转贴) asp.net控件开发基础(1)(转贴) C#中使用反射显示程序集的所有类型和属性(转贴)
spring:bean生命周期(转贴)
xiaolei1982 · 2008-06-14 · via 博客园 - xiaolei1982

分为定义,初始化,使用,消亡

写个例子测试一下:

第一步:建一个类User,代码如下:

java 代码

  1. package test.lyx;   
  2. publicclass User {   
  3.     private String userName;   
  4.     public String getUserName() {   
  5.         returnuserName;   
  6.     }   
  7.     publicvoid setUserName(String userName) {   
  8.         this.userName = userName;   
  9.     }   
  10. }   

第二步:将User注入.applicationContext.xml配置如下:

xml 代码

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.         <bean id="user" class="test.lyx.User" abstract="false"  
  5.         singleton="true" lazy-init="default" autowire="byType"  
  6.         dependency-check="default">  
  7.         <property name="userName">  
  8.             <value>liuyuanxi</value>  
  9.         </property>  
  10.     </bean>  
  11. </beans>  
第三步:建一个类TestMain测试一下:

java 代码

  1. package test.lyx;   
  2. import org.springframework.context.ApplicationContext;   
  3. import org.springframework.context.support.FileSystemXmlApplicationContext;   
  4. publicclass TestMain {   
  5.     publicstaticvoid main(String[] args) {   
  6.     ApplicationContext context=new FileSystemXmlApplicationContext("test/lyx/applicationContext.xml");   
  7.         User user1=(User)context.getBean("user");   
  8.         System.out.println(user1.getUserName());   
  9.     }   
  10. }  

这样运行该类会打出liuyuanxi.那么这几步就是bean的定义和使用.

接下来我们在User类中加上两个方法:init clear,当然方法明任你起.

java 代码

  1. publicvoid init(){   
  2.        this.userName="zhangsan";   
  3.    }   
  4.    publicvoid clear(){   
  5.        this.userName=null;   
  6.    }   

然后将配置文件修改如下:

xml 代码

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.         <bean id="user" class="test.lyx.User" abstract="false"  
  5.         singleton="true" lazy-init="default" autowire="byType"  
  6.         dependency-check="default" init-method="init" destroy-method="clear">  
  7.         <property name="userName">  
  8.             <value>liuyuanxi</value>  
  9.         </property>  
  10.     </bean>  
  11. </beans>  

这里面的红线部分就是修改的内容.如果init-method属性设置了方法,那么就会在bean初始化的时候被执行.而destroy-method在消毁之前执行.

第二种方法:(实现初始化,和消毁的两个接口)

可以不在配置文件中指定init-methoddestroy-method两个方法.

我们把User类修改一下:代码如下:

java 代码

  1. package test.lyx;   
  2. import org.springframework.beans.factory.InitializingBean;   
  3. import org.springframework.beans.factory.DisposableBean;   
  4. publicclass User implements InitializingBean,DisposableBean{   
  5.     private String userName;   
  6.     public String getUserName() {   
  7.         returnuserName;   
  8.     }   
  9.     publicvoid setUserName(String userName) {   
  10.         this.userName = userName;   
  11.     }   
  12.     publicvoid init(){   
  13.         this.userName="zhangsan";   
  14.     }   
  15.     publicvoid clear(){   
  16.         this.userName=null;   
  17.     }   
  18.     publicvoid afterPropertiesSet() throws Exception {   
  19.         this.userName="wangwu";// TODO Auto-generated method stub   
  20.     }   
  21.     
  22.     publicvoid destroy() throws Exception {   
  23.         this.userName=null;// TODO Auto-generated method stub   
  24.         }   
  25. }   

修改过后的类,就是在原来的基础上,实现两个接口InitializingBean,DisposableBean也就是初始化和消毁的接口,这里我们要把InitializingBean接口里的afterPropertiesSet方法给覆盖掉,也就是将初始化的东西写在这个方法以里面.同时也要把DisposableBean接口的destroy方法覆盖掉,

消毁的东西写在这个方法里.这样的话,就无需在配置文件中配置init-methoddestroy-method两个方法.

整个的过程就是bean的生命周期.