<ExecuteMethod.java>
package com.jetair.service;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* Created by 22431 on 2016/8/29.
*/
@Component
public class ExecuteMethod {
/**
* 容器启动则会执行该方法
* 添加postConstruct注解
*/
@PostConstruct
public void init(){
System.out.println("容器启动执行方法");
}
/**
* 容器关闭则会执行该方法
* 添加preDestroy注解
*/
@PreDestroy
public void destroy(){
System.out.println("容器关闭执行方法");
}
}
<ExecuteTest.java>
import com.jetair.service.ExecuteMethod;
import com.sun.org.apache.xpath.internal.SourceTree;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by 22431 on 2016/8/29.
*/
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationConfig.xml");
ExecuteMethod executeMethod = (ExecuteMethod)applicationContext.getBean("executeMethod");
executeMethod.destroy();
}
}