bean配置文件如下:
<bean id="user" class="com.cyb.ioc.User" init-method="myInit" destroy-method="myDestroy">
<property name="name" value="chenyuanbao"></property>
</bean>
<bean class="com.cyb.ioc.MyPostProcesser"> </bean>
<bean class="com.cyb.ioc.MyBeanFactoryAware"> </bean>
package com.cyb.ioc;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class User implements BeanNameAware,InitializingBean,DisposableBean{
String name ;
public User(){
System.out.println("new Bean默认的构造方法");
}
public void setName(String name) {
System.out.println("Bean.setName 自定义Bean设置属性值");
this.name = name;
}
public void showName(){
System.out.println("**********************************");
System.out.println("*****程序调用属性值:显示名称 "+name+"******");
System.out.println("**********************************");
}
public void myInit(){
System.out.println("init-method 自定义初始化方法");
}
public void myDestroy(){
System.out.println("destroy-method 自定义销毁方法");
}
public void setBeanName(String arg0) {
System.out.println("BeanNameAware.setBeanName "+arg0);
}
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean.afterPropertiesSet 容器调用");
}
public void destroy() throws Exception {
System.out.println("DisposableBean.destroy 容器的销毁方法");
}
}
执行结果如下:
25, 2017 8:10:31 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1766de66: startup date [Wed Oct 25 20:10:31 CST 2017]; root of context hierarchy十月 25, 2017 8:10:31 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext_bean.xml]
new Bean默认的构造方法
Bean.setName 自定义Bean设置属性值
BeanNameAware.setBeanName user
Bean前置处理器 com.cyb.ioc.User@4bf7a31f,user
InitializingBean.afterPropertiesSet 容器调用
init-method 自定义初始化方法
Bean后置处理器 com.cyb.ioc.User@4bf7a31f,user
BeanFactoryAware com.cyb.ioc.User@4bf7a31f
**********************************
*****程序调用属性值:显示名称 chenyuanbao******
**********************************
十月 25, 2017 8:10:31 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1766de66: startup date [Wed Oct 25 20:10:31 CST 2017]; root of context hierarchy
DisposableBean.destroy 容器的销毁方法
destroy-method 自定义销毁方法