1、bean对象
private String name; public String getName() { return name; } public void setName(String name) { System.out.println("2、setname..."); this.name = name; } public User() { System.out.println("1、无参构造..."); } public void initUser(){ System.out.println("4、初始化..."); } public void destroyUser(){ System.out.println("7、销毁..."); }
2、bean后置处理器
public class BeanPost implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) { System.out.println("3、后置处理器,初始化之前..."); System.out.println(beanName+"::"+bean); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) { System.out.println("5、后置处理器,初始化之后..."); System.out.println(beanName+"::"+bean); return bean; } }
3、配置bean.xml
<bean id="user" class="com.jixian.spring.entity.User" init-method="initUser" destroy-method="destroyUser"> <property name="name" value="jixian"/> </bean> <bean id="beanPost" class="com.jixian.spring.entity.BeanPost"/>
4、测试
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml"); User user = applicationContext.getBean(User.class); System.out.println("6、bean对象创建完成..."); System.out.println(user); applicationContext.close();
标签:...,生命周期,System,bean,println,public,out From: https://www.cnblogs.com/jixian/p/17156829.html