SimpleBean.java
InitializingBean接口中只有一个afterPropertiesSet方法
package
ch5.initMethod;
import org.springframework.beans.factory.InitializingBean;
public class SimpleBean implements InitializingBean ... {
public void afterPropertiesSet() throws Exception ...{
System.out.println("int afterPropertiesSet");
}
private String name;
private String sex;
private String age;
public void init()...{
System.out.println("this is info from constructor from SimpleBean and property is name:"+this.getName());
}
public String getAge() ...{
return age;
}
public void setAge(String age) ...{
this.age = age;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getSex() ...{
return sex;
}
public void setSex(String sex) ...{
this.sex = sex;
}
}
import org.springframework.beans.factory.InitializingBean;
public class SimpleBean implements InitializingBean ... {
public void afterPropertiesSet() throws Exception ...{
System.out.println("int afterPropertiesSet");
}
private String name;
private String sex;
private String age;
public void init()...{
System.out.println("this is info from constructor from SimpleBean and property is name:"+this.getName());
}
public String getAge() ...{
return age;
}
public void setAge(String age) ...{
this.age = age;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getSex() ...{
return sex;
}
public void setSex(String sex) ...{
this.sex = sex;
}
}
配置文件:
<?
xml version="1.0" encoding="UTF-8"
?>
< beans
xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
< bean id ="SimpleBean" class ="ch5.initMethod.SimpleBean" init-method ="init" >
< property name ="name" >
< value > gaoxiang </ value >
</ property >
< property name ="sex" >
< value > male </ value >
</ property >
< property name ="age" >
< value > 26 </ value >
</ property >
</ bean >
</ beans >
< beans
xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
< bean id ="SimpleBean" class ="ch5.initMethod.SimpleBean" init-method ="init" >
< property name ="name" >
< value > gaoxiang </ value >
</ property >
< property name ="sex" >
< value > male </ value >
</ property >
< property name ="age" >
< value > 26 </ value >
</ property >
</ bean >
</ beans >
测试代码:
package
ch5.initMethod;
import java.io.File;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestSpring ... {
public static void main(String args[]) throws Exception...{
//获取bean factory
BeanFactory factory=(BeanFactory)getBeanFactory(); //使用子beanFactory
SimpleBean bean1=(SimpleBean)factory.getBean("SimpleBean");
}
public static BeanDefinitionRegistry getBeanFactory()...{
//获取bean factory
String realpath="";
//加载配置项
realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch5/initMethod"+File.separator+"applicationContext.xml";
XmlBeanFactory factory=new XmlBeanFactory(new FileSystemResource(realpath));
return factory;
}
}
import java.io.File;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestSpring ... {
public static void main(String args[]) throws Exception...{
//获取bean factory
BeanFactory factory=(BeanFactory)getBeanFactory(); //使用子beanFactory
SimpleBean bean1=(SimpleBean)factory.getBean("SimpleBean");
}
public static BeanDefinitionRegistry getBeanFactory()...{
//获取bean factory
String realpath="";
//加载配置项
realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch5/initMethod"+File.separator+"applicationContext.xml";
XmlBeanFactory factory=new XmlBeanFactory(new FileSystemResource(realpath));
return factory;
}
}
运行结果:
this is info from afterpropertiesSet from SimpleBean and property is name:gaoxiang
this is info from constructor from SimpleBean and property is name:gaoxiang
可以看initializingBean的生命周期是设置属性之后,自定义init-method之前运行
这种方式的好处是一次就能为bean的所有实例指定初始化回调,代价是应用程序和spring耦合