文件结构:
package com.zxl.spring;
public class MethodHooksBean {
public void doInitialize(){
System.out.println("methodhooksbean -> doInitialize init-method invoked");
}
public void doDestroy(){
System.out.println("methodhooksbean -> doDestroy destroy-method invoked");
}
}
package com.zxl.spring;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class PostProcessorsBean implements InitializingBean,DisposableBean {
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("postprocessorsbean -> afterpropertiesset method invoked");
}
@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("postprocessorsbean -> destroy method invoked");
}
}
package com.zxl.spring;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApp07 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("SpringBeans.xml");
context.close();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="postProcessorsBean" class="com.zxl.spring.PostProcessorsBean" />
<bean id="methodHooksBean" class="com.zxl.spring.MethodHooksBean" init-method="doInitialize" destroy-method="doDestroy" />
</beans>