我是弹簧新手。我正在尝试为不同的bean调用调用init和destroy方法
我的 init 方法仅从“FileSystemResource”调用 为什么没有从其他人调用?
public class DefaultMessage {
private String message = "Basic Bean injecting";
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public DefaultMessage(String message) {
this.message = message;
}
public DefaultMessage() {
}
public void init(){
System.out.println("Testing init.");
}
public void destory(){
System.out.println("Spring Container is destroyed.");
}
我的 XML 是
<bean id="basicBean" class="com.sarma.spring.core.DefaultMessage" init-method="init" destroy-method="destory"></bean>
主班
//Type 1
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("DefaultMessage.xml");
//Basic Bean Testing
DefaultMessage message = (DefaultMessage) applicationContext.getBean("basicBean");
log.info(message.getMessage());
log.info("---------------------------ApplicationContext End------------------------------");
//Type 2
Resource res = new FileSystemResource("C:\\Sarma\\Spring\\SpringEx\\src\\resource\\DefaultMessage.xml");
BeanFactory factory = new XmlBeanFactory(res);
DefaultMessage message1 = (DefaultMessage) factory.getBean("basicBean");
log.info("Test "+message1.getMessage());
log.info("- ---------------------------FileSystemResource End------------------------------");
//Type 3
ClassPathResource res1 = new ClassPathResource("DefaultMessage.xml");
BeanFactory factory1 = new XmlBeanFactory(res1);
DefaultMessage message2 = (DefaultMessage) factory1.getBean("basicBean");
log.info("Test "+message2.getMessage());
log.info("- ---------------------------ClassPathResource End-------------------------------");
//Type4
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"DefaultMessage.xml"});
DefaultMessage message3 = (DefaultMessage) context.getBean("basicBean");
log.info(message3.getMessage());
context.close();
log.info("- ---------------------------ConfigurableApplicationContext End------------------");
输出
2013-09-18 13:49:22 INFO ClassPathXmlApplicationContext:513 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@913750: startup date [Wed Sep 18 13:49:22 EDT 2013]; root of context hierarchy
2013-09-18 13:49:22 INFO XmlBeanDefinitionReader:316 - Loading XML bean definitions from class path resource [DefaultMessage.xml]
2013-09-18 13:49:22 INFO DefaultMessageMain:27 - Basic Bean injecting
2013-09-18 13:49:22 INFO DefaultMessageMain:29 - ---------------------------ApplicationContext End------------------------------
2013-09-18 13:49:22 INFO XmlBeanDefinitionReader:316 - Loading XML bean definitions from file [C:\Sarma\Spring\SpringEx\src\resource\DefaultMessage.xml]
Testing init.
2013-09-18 13:49:22 INFO DefaultMessageMain:36 - Test Basic Bean injecting
2013-09-18 13:49:22 INFO DefaultMessageMain:37 - - ---------------------------FileSystemResource End------------------------------
2013-09-18 13:49:22 INFO XmlBeanDefinitionReader:316 - Loading XML bean definitions from class path resource [DefaultMessage.xml]
2013-09-18 13:49:22 INFO DefaultMessageMain:42 - Test Basic Bean injecting
2013-09-18 13:49:22 INFO DefaultMessageMain:43 - - ---------------------------ClassPathResource End-------------------------------
2013-09-18 13:49:22 INFO ClassPathXmlApplicationContext:513 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@176e552: startup date [Wed Sep 18 13:49:22 EDT 2013]; root of context hierarchy
2013-09-18 13:49:22 INFO XmlBeanDefinitionReader:316 - Loading XML bean definitions from class path resource [DefaultMessage.xml]
2013-09-18 13:49:22 INFO DefaultMessageMain:47 - Basic Bean injecting
2013-09-18 13:49:22 INFO ClassPathXmlApplicationContext:873 - Closing org.springframework.context.support.ClassPathXmlApplicationContext@176e552: startup date [Wed Sep 18 13:49:22 EDT 2013]; root of context hierarchy
2013-09-18 13:49:22 INFO DefaultMessageMain:49 - - ---------------------------ConfigurableApplicationContext End------------------
我的 init 方法仅从“FileSystemResource”调用 为什么没有从其他人调用?
为什么它的行为不同?
它从来没有调用过我的销毁方法