本文共 3545 字,大约阅读时间需要 11 分钟。
package com.xiaochuange.platform.spring4;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;/** * @Desc: * @Auther: spring * @Date: 2018-9-12 14:56 */@Component("initAndDestoryService")public class InitAndDestoryService { // 销毁之前执行 @PreDestroy public void methodPreDestroy() { System.out.println("InitAndDestoryService @PreDestroy invoke..."); } // 初始化之前执行 @PostConstruct public void methodPostConstruct() { System.out.println("InitAndDestoryService @PostConstruct invoke..."); }}
package com.xiaochuange.platform.spring4;/** * @Desc: * @Auther: spring * @Date: 2018-9-12 15:03 */public class XmlInitAndDestoryService { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void methodInit(){ System.out.println("XmlInitAndDestoryService methodInit invoke..." + message); } public void methodDestory(){ System.out.println("XmlInitAndDestoryService methodDestory invoke..." + message); }}
applicationContext.xml
package com.xiaochuange.platform.spring4;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;/** * @Desc: * @Auther: spring * @Date: 2018-9-12 14:27 */public class InitializingBeanAndDisposableBeanService implements InitializingBean, DisposableBean { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @Override public void destroy() throws Exception { // 销毁时的操作 System.out.println("InitializingBeanAndDisposableBeanService destroy() invoke..." + message); } @Override public void afterPropertiesSet() throws Exception { // 初始化时的操作 System.out.println("InitializingBeanAndDisposableBeanService afterPropertiesSet() invoke..." + message); }}
applicationContext.xml
package com.xiaochuange.platform.spring4;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * @Desc: * @Auther: spring * @Date: 2018-9-12 14:45 */public class MainTest { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); InitializingBeanAndDisposableBeanService personService = (InitializingBeanAndDisposableBeanService) context.getBean("personService"); InitAndDestoryService initAndDestoryService = (InitAndDestoryService) context.getBean("initAndDestoryService"); XmlInitAndDestoryService xmlInitAndDestoryService = (XmlInitAndDestoryService) context.getBean("xmlInitAndDestoryService"); context.registerShutdownHook(); // 输出如下:// InitAndDestoryService @PostConstruct invoke...// InitializingBeanAndDisposableBeanService afterPropertiesSet() invoke...123// XmlInitAndDestoryService methodInit invoke...234// XmlInitAndDestoryService methodDestory invoke...234// InitializingBeanAndDisposableBeanService destroy() invoke...123// InitAndDestoryService @PreDestroy invoke... }}
转载于:https://blog.51cto.com/springsupervip/2174254