1、首先用图片的方式给大家展示一下大致的流程。
2、代码
package cn.spring.demo02;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class Person implements DisposableBean, InitializingBean, BeanNameAware,
ApplicationContextAware {
public Person() {
System.err.println("1:类被初始化成功了...");
}
private String name;
public void setName(String name) {
System.err.println("2:设置属性...");
this.name = name;
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
System.err.println("4:设置applicationcontext");
}
public void ddd() {
System.err.println("8:生命周期结束");
}
@Override
public void setBeanName(String name) {
System.err.println("3:设置bean的名称");
}
@Override
public void afterPropertiesSet() throws Exception {
System.err.println("5:属性设置完成了。。");
}
public void init() {
System.err.println("6:调用用户配置的init-method");
}
public void destroy() throws Exception {
System.err.println("7:对象开始被销毁了..");
}
}
配置:
<bean id="person" destroy-method="ddd" class="cn.spring.demo02.Person" init-method="init">
<property name="name" value="Jack"></property>
</bean>
以下是结果:
这里的