为什么spring init-method destroy-method方法表现不同?

时间:2013-09-18 17:59:05

标签: java spring

我是新手。我试图为不同的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”调用为什么不从其他人调用它?

为什么表现不同?

它从未调用过我的破坏方法

3 个答案:

答案 0 :(得分:1)

我要复制你的代码来解释

//Type 1
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("DefaultMessage.xml");

//Basic Bean Testing
DefaultMessage message = (DefaultMessage) applicationContext.getBean("basicBean");

以上将init()你的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");

这也是init()你的bean。

//Type 3
ClassPathResource res1 = new ClassPathResource("DefaultMessage.xml");
BeanFactory factory1 = new XmlBeanFactory(res1);
DefaultMessage message2 = (DefaultMessage) factory1.getBean("basicBean");

这也是init()你的bean。

//Type4
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"DefaultMessage.xml"});
DefaultMessage message3 = (DefaultMessage) context.getBean("basicBean");
log.info(message3.getMessage());
context.close();

这也将init()你的bean。由于context.close(),所有bean将在关闭上下文之前被销毁。如果没有调用destroy-method,你可以尝试使用DisposableBean接口,但考虑到这种不好的做法,因为你的类开始依赖于Spring类型。

答案 1 :(得分:0)

我已经为我的bean类实现了DisposableBean接口,并覆盖了destroy方法,并从我的XML中删除了destroy-method方法。

现在destroy方法调用3次,除了ApplicationContext, Destroy方法是从FileSystemResource,ClassPathResource,ClassPathXmlApplicationContext调用而不是从ApplicationContext调用。

答案 2 :(得分:0)

对于ClassPathXmlApplicationContext,我们可以传递不同的xml,例如

ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"DefaultMessage.xml", "DefaultMessage1.xml}");

关闭ConfigurableApplicationContext时,它将关闭所有打开的资源,这就是它关闭所有3个FileSystemResource,ClassPathResource,ClassPathXmlApplicationContext的原因。