bean的生命周期


bean的定义
<?xml version="1.0" encoding="UTF-8"?>
<!-- 此引用需要联网才可以 -->
<!-- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd"> -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 设置为单例模式singleton 原型值设置为prototype -->
<!-- bean初始化方法init-method 依赖注入就可以注释掉-->
<!-- bean销毁 通过destroy-method属性来完成 -->
<!-- <bean id="HelloWorld" class="com.lsw.action.HelloWorld" scope="singleton" init-method="init" destroy-method="cleanup"> -->
<bean id="HelloWorld" class="com.lsw.action.HelloWorld" init-method="init">
<!-- date变量通过依赖注入来完成 -->
<!-- <property name="msg">
<value>Hello World you</value>
</property>
<property name="date">
<bean id="date" class="java.util.Date"/>
</property> -->
</bean>
</beans>

执行报错 cvc-complex-type.3.2.2: Attribute 'singleton' is not allowed to appear in element 'bean'.
原因是spring4.0的版本中没有singleton 最新版本默认是scope
<bean id="HelloWorld" class="com.lsw.action.HelloWorld" scope="singleton">
static final String SCOPE_SESSION
Scope identifier for session scope: "session". Supported in addition to the standard scopes "singleton" and "prototype".
See Also: Constant Field Values
bean的初始化
/*
* bean初始化得方法
* 第一种:使用init()来完成初始化工作
* 第二种:实现org.springframework.beans.factory.InitializingBean接口
*/
public void init(){
this.msg = "Welcome to you";
this.date = new Date();
}

public void afterPropertiesSet(){
this.msg = "Welcome to afterPropertiesSet";
this.date = new Date();
}
bean的使用
/*
* bean的使用方式
* 第一种:使用BeanWrapper
* 第二种:使用BeanFactory 这种方法试了之后发现运行报错 给出的解释是不建议使用
* 第种:使用ApplicationContext
*/
/* HelloWorld helloworld = new HelloWorld();
BeanWrapper bw = new BeanWrapperImpl(helloworld);
bw.setPropertyValue("msg", "BeanWrapper");
System.out.println(bw.getPropertyValue("msg"));*/

/* InputStream is = new FileInputStream("config.xml");
BeanFactory factory = new XmlBeanFactory((Resource) is);
HelloWorld hw = (HelloWorld)factory.getBean("HelloWorld");
System.out.println(hw.getMsg() + hw.getDate());*/

ApplicationContext actx = new FileSystemXmlApplicationContext("config.xml");
HelloWorld hw = (HelloWorld)actx.getBean("HelloWorld");
System.out.println(hw.getMsg() + hw.getDate());
//如果不加上此方法,destroy将不会执行
((FileSystemXmlApplicationContext)actx).close();
bean的销毁
/*
* bean销毁
* 第一种:使用destroy-method属性来完成
* 第二种:实现org.springframework.beans.factory.DisposableBean接口
*/
public void cleanup(){
this.msg = "";
this.date = null;
System.out.println("cleanup" + this.msg + this.date);
}

public void destroy() throws Exception {
this.msg = "";
this.date = null;
System.out.println("destroy" + this.msg + this.date);
}

完整的实体类
package com.lsw.action;

import java.util.Date;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class HelloWorld implements InitializingBean,DisposableBean{
public String msg = null;
private Date date = null;

/*
* bean初始化得方法
* 第一种:使用init()来完成初始化工作
* 第二种:实现org.springframework.beans.factory.InitializingBean接口
*/
public void init(){
this.msg = "Welcome to you";
this.date = new Date();
}

public void afterPropertiesSet(){
this.msg = "Welcome to afterPropertiesSet";
this.date = new Date();
}

/*
* bean销毁
* 第一种:使用destroy-method属性来完成
* 第二种:实现org.springframework.beans.factory.DisposableBean接口
*/
public void cleanup(){
this.msg = "";
this.date = null;
System.out.println("cleanup" + this.msg + this.date);
}

public void destroy() throws Exception {
this.msg = "";
this.date = null;
System.out.println("destroy" + this.msg + this.date);
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}
}

原文地址:https://www.cnblogs.com/batman425/p/7529141.html