Spring bean destroy-method,singleton和prototype范围 [英] Spring bean destroy-method , singleton and prototype scopes

查看:110
本文介绍了Spring bean destroy-method,singleton和prototype范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring框架的新手,从一些教程开始学习它。

I am new to the spring framework, started with some tutorials to learn it.

我有以下文件,

#MainProgram.java

# MainProgram.java

package test.spring;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainProgram {
        public static void main(String[] args) {
              AbstractApplicationContext context = 
                              new ClassPathXmlApplicationContext("Bean.xml");     
              HelloSpring obj = (HelloSpring) context.getBean("helloSpring");
              obj.setMessage("My message");
              obj.getMessage();
              context.registerShutdownHook();

        }
 }

#HelloSpring.java

# HelloSpring.java

package test.spring;

public class HelloSpring   {
     private String message;

     public void setMessage(String message){
      this.message  = message;
      System.out.println("Inside setMessage");
   }

   public void getMessage(){
      System.out.println("Your Message : " + this.message);
   }

   public void xmlInit() {
    System.out.println("xml configured  initialize");
   } 

    public void xmlDestroy() {
    System.out.println("xml configured destroy");
    }

  }

#Bean.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

     <bean id="helloSpring" class="test.spring.HelloSpring" 
          scope="prototype" init-method="xmlInit" destroy-method="xmlDestroy">

     </bean>
     </beans>

当我拿 scope =singleton我的输出是:

 xml configured  initialize
 Inside setMessage
 Your Message : My message
 xml configured destroy

当我拿 scope =prototype我的输出是:

 xml configured  initialize
 Inside setMessage
 Your Message : My message

xmlDestroy()使用<$调用方法c $ c> singleton 范围bean但没有原型
请帮助我以下,

xmlDestroy() method is called with singleton scope bean but not with prototype kindly help me for the following ,

这是对的吗?如果是这样,可能的原因是什么?

Is this correct? if so, what would be possible reasons?

我也有一些问题,如

有什么区别或者
之间的关系ApplicationContext,AbstractApplicationContext和ClassPathXmlApplicationContext

推荐答案

xmlDestroy()使用单例范围bean调用方法但不使用原型调用方法,因为

xmlDestroy() method is called with singleton scope bean but not with prototype because

Spring无法管理整个生命周期原型bean:容器实例化,配置,装饰和组装原型对象,将其交给客户端,然后不再了解该原型实例。为了释放资源,尝试实现自定义bean后处理器。

Spring does not manage the complete lifecycle of a prototype bean: the container instantiates, configures, decorates and otherwise assembles a prototype object, hands it to the client and then has no further knowledge of that prototype instance. For releasing resources try to implement a custom bean post processor.

与弹簧容器管理完整生命周期的单件bean不同

Unlike singleton beans where the spring container manages the complete life-cycle

你可以看看这个关于不同背景之间差异的基本教程

You can have a look at this basic tutorial for differences between different contexts

参考文档

这篇关于Spring bean destroy-method,singleton和prototype范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆