복붙노트

[SPRING] 스프링 프레임 워크에서 메소드가 작동하지 않습니다. [duplicate]

SPRING

스프링 프레임 워크에서 메소드가 작동하지 않습니다. [duplicate]

편집하다: 이 질문은 context.registerShutdownHook을 제대로 호출하고 있기 때문에 destroy 메소드가 호출 될 때와 같지 않습니다. 로그에서 볼 수 있듯이 bean이 파괴되고 있습니다. 제 문제는 봄이 제 방법을 부르지 않는다는 것입니다. 나는이 질문을하기 전에 여기에서 확인했다.

스프링 프레임 워크를 사용하여 응용 프로그램에서 우아한 파괴를 구성하고 있습니다. 프로그램을 실행할 때 bean.xml에 지정된 destory 메소드를 호출하지 않습니다. 내가 뭘 잘못하고 있는지 도와주세요.

SSCCE입니다.

Ben.hml

<?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.springframework.context.support.ClassPathXmlApplicationContext@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.springframework.beans.factory.support.DefaultListableBeanFactory@63390b47: defining beans [helloworld]; root of factory hierarchy
May 27, 2013 11:59:14 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@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.springframework.beans.factory.support.DefaultListableBeanFactory@63390b47: defining beans [helloworld]; root of factory hierarchy

개정: 나는 close와 registerShutdownHook ()을 시도하여 문맥을 닫았으며 아무 것도 작동하지 않는다.

해결법

  1. ==============================

    1.Destroy 메소드는 범위 프로토 타입의 bean에 대해 호출되지 않습니다. 이는 컨텍스트가 프로토 타입 범위 객체를 추적하지 않기 때문입니다 (그렇다면 스프링이 처리 할시기를 알 수 없으므로 메모리 누수가 발생합니다).

    Destroy 메소드는 범위 프로토 타입의 bean에 대해 호출되지 않습니다. 이는 컨텍스트가 프로토 타입 범위 객체를 추적하지 않기 때문입니다 (그렇다면 스프링이 처리 할시기를 알 수 없으므로 메모리 누수가 발생합니다).

    봄 문서의 세부 사항.

    Spring 참조 문서

  2. from https://stackoverflow.com/questions/16783552/destroy-method-is-not-working-in-spring-framework by cc-by-sa and MIT license