Reputation: 909
I am new to springs. I am trying to call init and destroy method for different bean invoking
My init method is called from only "FileSystemResource" Why it was not called from others?
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.");
}
My XML is
<bean id="basicBean" class="com.sarma.spring.core.DefaultMessage" init-method="init" destroy-method="destory"></bean>
Main class
//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------------------");
OUTPUT
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------------------
My init method is called from only "FileSystemResource" Why it was not called from others?
Why it is behaving different?
It never called my destroy method
Upvotes: 0
Views: 1254
Reputation:
For ClassPathXmlApplicationContext, we can pass different xml's for example
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"DefaultMessage.xml", "DefaultMessage1.xml}");
while closing ConfigurableApplicationContext it will close all resource which was opened, that why it closed all 3 FileSystemResource, ClassPathResource, ClassPathXmlApplicationContext.
Upvotes: 0
Reputation: 909
I have implemented DisposableBean interface to my bean class and overrides destroy method and removed destroy-method method form my XML.
Now destroy method is calling 3 times except ApplicationContext, Destroy method is calling from FileSystemResource, ClassPathResource, ClassPathXmlApplicationContext and not from ApplicationContext.
Upvotes: 0
Reputation: 280011
I'm going to copy your code to explain
//Type 1
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("DefaultMessage.xml");
//Basic Bean Testing
DefaultMessage message = (DefaultMessage) applicationContext.getBean("basicBean");
The above will init()
your bean.
//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");
This will also init()
your bean.
//Type 3
ClassPathResource res1 = new ClassPathResource("DefaultMessage.xml");
BeanFactory factory1 = new XmlBeanFactory(res1);
DefaultMessage message2 = (DefaultMessage) factory1.getBean("basicBean");
This will also init()
your bean.
//Type4
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"DefaultMessage.xml"});
DefaultMessage message3 = (DefaultMessage) context.getBean("basicBean");
log.info(message3.getMessage());
context.close();
This will also init()
your bean. Because of the context.close()
, all beans will be destroyed before the context being closed. If the destroy-method
isn't being called, you can try with DisposableBean
interface, but consider that bad practice as your classes start being dependent on Spring types.
Upvotes: 1