【发布时间】:2018-08-06 20:05:04
【问题描述】:
我有一个配置简单的基本 spring 项目。这是豆子
public class GreetingServiceImpl implements GreetingService {
private final Log log = LogFactory.getLog(getClass());
@Override
public String hello() {
return "Hello";
}
private void init() {
log.info("GreetingServiceImpl INIT");
}
private void destroy() {
log.info("GreetingServiceImpl DESTROY");
}
}
配置:
<bean id="greetingService"
class="com.example.hello.GreetingServiceImpl"
init-method="init"
destroy-method="destroy">
这是我的测试代码:
@Test
public void greeting() {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationConfig.xml");
GreetingService greetingService = context.getBean(GreetingService.class);
Assert.assertEquals("Hello", greetingService.hello());
context.close();
}
当我运行此代码时,我在日志中看不到 destroy 方法以及上下文关闭。
org.springframework.context.support.AbstractApplicationContext prepareRefresh
Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2be94b0f
org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
Loading XML bean definitions from class path resource [applicationConfig.xml]
com.example.hello.GreetingServiceImpl init
GreetingServiceImpl INIT
Process finished with exit code 0
我尝试调用registerShutdownHook 和refresh,但结果相同。
【问题讨论】:
标签: java spring lifecycle predestroy