2013-05-28 10 views
7

संपादित करें: यह प्रश्न When does destroy method is called जैसा नहीं है क्योंकि मैं context.registerShutdownHook पर ठीक से कॉल कर रहा हूं और मेरे बीन लॉग इन से देख सकते हैं क्योंकि आप लॉग से देख सकते हैं। मेरी समस्या वसंत मेरी विधि को नहीं बुला रही है। मैंने यहां पूछने से पहले इस सवाल की जांच की है।विध्वंस विधि वसंत ढांचे में काम नहीं कर रही है

मैं वसंत ढांचे का उपयोग कर अपने आवेदन में सुंदर विनाश को विन्यस्त कर रहा हूं। जब मैं प्रोग्राम चला रहा हूं तो यह bean.xml में निर्दिष्ट destory विधि को कॉल नहीं कर रहा है। कृपया मेरी मदद करें मैं गलत क्या कर रहा हूं।

यहाँ

SSCCE

है 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.xsd"> 

    <bean id="helloworld" class="com.hello.pojo.HelloWorld" 
      scope="prototype" init-method="init" destroy-method="destroy"> 
    </bean> 

</beans> 

HelloWord.java

package com.hello.pojo; 

public class HelloWorld { 


    private String message; 

    public String getMessage() { 
     return message; 
    } 

    public void setMessage(String message) { 
     this.message = message; 
    } 

    public void init(){ 
     System.out.println("Bean initializating is in progress"); 
    } 

    public void printMessage(){ 
     System.out.println("Your message: "+getMessage()); 
    } 
    public void destroy(){ 
     System.out.println("Bean is being destroyed"); 
    } 

} 

MainApp.java

package com.main; 

import org.springframework.context.support.AbstractApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.hello.pojo.HelloWorld; 


public class MainApp { 

    public static void main(String[]args){ 
     AbstractApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml"); 
     HelloWorld objA = (HelloWorld) context.getBean("helloworld"); 
     objA.setMessage("I am Object A"); 
     objA.printMessage(); 
     context.registerShutdownHook(); 
    } 

} 

आउटपुट

May 27, 2013 11:59:14 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 
INFO: Refreshing org[email protected]e9028874: startup date [Mon May 27 23:59:14 EDT 2013]; root of context hierarchy 
May 27, 2013 11:59:14 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [Bean.xml] 
Bean initializating is in progress 
Your message: I am Object A 
May 27, 2013 11:59:14 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 
INFO: Pre-instantiating singletons in org.s[email protected]63390b47: defining beans [helloworld]; root of factory hierarchy 
May 27, 2013 11:59:14 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose 
INFO: Closing org[email protected]e9028874: startup date [Mon May 27 23:59:14 EDT 2013]; root of context hierarchy 
May 27, 2013 11:59:14 PM org.springframework.beans.factory.support.DefaultListableBeanFactory destroySingletons 
INFO: Destroying singletons in org.s[email protected]63390b47: defining beans [helloworld]; root of factory hierarchy 

संशोधन: मैं close और registerShutdownHook() की कोशिश की है संदर्भ बंद करने के लिए और उनमें से कोई काम करता है।

+0

@ मैडप्रोग्रामर मुझे लगता है कि हम बीन.एक्सएमएल में किसी भी नाम के साथ विधि को कॉन्फ़िगर कर सकते हैं जब तक कि दोनों का एक ही नाम हो। हालांकि मैंने विनाश के साथ भी कोशिश की और यह काम नहीं कर रहा है – Ashish

+0

@ मैडप्रोग्रामर मुझे लगता है कि आपका मतलब है 'नष्ट', 'विनाश' नहीं;) – Vulcan

+0

@ वल्कन मुझे लगता है कि मेरा मतलब बीयर है;) – MadProgrammer

उत्तर

32

स्कोप प्रोटोटाइप के बीन्स के लिए विधियों को नष्ट नहीं किया जाता है। ऐसा इसलिए है क्योंकि संदर्भ प्रोटोटाइप स्कोप ऑब्जेक्ट्स का ट्रैक नहीं रखता है (यदि ऐसा होता है, तो यह स्मृति मेमोरी का कारण बनता है क्योंकि वसंत को यह नहीं पता कि इसे कब निपटाना है)।

वसंत दस्तावेज़ीकरण से विवरण।

Spring reference documentation

एक काफी महत्वपूर्ण है जब प्रोटोटाइप दायरे में एक सेम की तैनाती, कि में सेम के जीवन चक्र परिवर्तनों में कुछ के बारे में पता होना करने के लिए चीज नहीं है। वसंत एक प्रोटोटाइप बीन के पूर्ण जीवन चक्र का प्रबंधन नहीं करता है: कंटेनर तत्काल, कॉन्फ़िगर, सजाने और अन्यथा प्रोटोटाइप ऑब्जेक्ट को इकट्ठा करता है, इसे क्लाइंट को सौंपता है और उसके बाद उस प्रोटोटाइप उदाहरण के बारे में और जानकारी नहीं है। इसका मतलब यह है कि प्रारंभिक जीवनशैली कॉलबैक विधियों को प्रोटोटाइप के मामले में सभी ऑब्जेक्ट्स पर कॉल किया जाएगा, प्रोटोटाइप के मामले में, किसी भी कॉन्फ़िगर किए गए विनाश जीवन चक्र कॉलबैक नहीं कहा जाएगा।

+0

बहुत बहुत धन्यवाद, मैंने प्रोटोटाइप हटा दिया और यह उम्मीद के अनुसार काम करना शुरू कर दिया। – Ashish

+0

बहुत बहुत धन्यवाद –

संबंधित मुद्दे