0

我是弹簧新手。我正在尝试为不同的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”调用 为什么没有从其他人调用?

为什么它的行为不同?

它从来没有调用过我的销毁方法

4

3 回答 3

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 类型。

于 2013-09-18T18:19:02.640 回答
0

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

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

于 2013-09-19T14:53:17.083 回答
0

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

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

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

于 2013-09-19T15:18:16.017 回答