본문 바로가기

Web/spring

[Spring Framework core] 1.6. Customizing the Nature of a Bean (1)

728x90

https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-nature

 

Core Technologies

In the preceding scenario, using @Autowired works well and provides the desired modularity, but determining exactly where the autowired bean definitions are declared is still somewhat ambiguous. For example, as a developer looking at ServiceConfig, how do

docs.spring.io

 

틀린 해석이 있다면 알려주세요 ☕️

 

1.6. Customizing the Nature of a Bean ~ Destruction Callbacks


1.6. Customizing the Nature of a Bean

Spring Framework는 bean 특성을 customize 할 수 있도록 여러 인터페이스를 제공한다

 

  • Lifecycle Callbacks 
  • ApplicationContextAware and BeanNameAware 
  • Other Aware Interfaces

 

이 세가지에 대해 알아본다

 

 

1.6.1. Lifecycle Callbacks

Bean lifecycle container 관리와 상호작용하려면 Spring InitializingBean 및 DisposableBean 인터페이스를 구현하면 된다.

container는 InitializingBean에 대해 afterPropertiesSet() 을 호출하고

DisposableBean에 대해 destroy() 를 호출하여 Bean이 initialization, destruction 하도록 한다

Using @PostConstruct and @PreDestroy.

 

Core Technologies

In the preceding scenario, using @Autowired works well and provides the desired modularity, but determining exactly where the autowired bean definitions are declared is still somewhat ambiguous. For example, as a developer looking at ServiceConfig, how do

docs.spring.io

 

 

내부적으로 Spring Framework는 BeanPostProcessor 구현을 사용하여 적절한 메소드를 찾고 호출할 수 있는 모든 callback interface를 처리한다. 기본적으로 제공하지않는 customize 기능이나 기타 수명 동작이 필요할 경우 BeanPostProcessor를 이용하면 된다. 

https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-extension

 

Core Technologies

In the preceding scenario, using @Autowired works well and provides the desired modularity, but determining exactly where the autowired bean definitions are declared is still somewhat ambiguous. For example, as a developer looking at ServiceConfig, how do

docs.spring.io

 

 

In addition to the initialization and destruction callbacks, Spring-managed objects may also implement the Lifecycle interface so that those objects can participate in the startup and shutdown process, as driven by the container’s own lifecycle.

The lifecycle callback interfaces are described in this section.

 

 
Initialization Callbacks

org.springframework.beans.factory.InitializingBean interface 는 컨테이너가 bean에 필요한 모든 속성을 설정한 후 bean이 초기화 작업을 수행하도록 한다. Initializing 인터페이스는 단일 메서드를 지정한다

 

void afterPropertiesSet() throws Exception;

 

불필요하게 이 인터페이스를 사용하지않는 것이 좋다.

또한 @PostConstruct 애노테이션을 사용하거나 POJO 초기화 방법을 지정하는게 좋다

XML 기반 구성 meta data의 경우 init-method 특성을 사용하여 무효인 인수 서명이 없는 메서드의 이름을 지정할 수 있다

Java 구성으로 @Bean의 initMethod 속성을 사용할 수 있다.

 

 

 

얘는 스프링에 연결하지않음

<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>

 

 

public class ExampleBean {

    public void init() {
        // do some initialization work
    }
}

이렇게 init() method 특성을 사용하여 이름 지정

 

 

거의 동일한 효과의 방법

<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
 
public class AnotherExampleBean implements InitializingBean {

    @Override
    public void afterPropertiesSet() {
        // do some initialization work
    }
}

 

 

Destruction Callbacks

org.springframework.beans.factory.DisposableBean interface를 구현하면 Bean을 포함하는 컨테이너가 destroy될 때 콜백을 얻을 수 있다. DisposableBean 인터페이스 또한 단일 메서드로 지정한다

 

void destroy() throws Exception;

이 또한 불필요하게 코드를 연결하기 때문에

이 대신

 

@PreDestroy 애노테이션이나 빈 정의에서 지원하나ㅡㄴ 일반 메소드를 추천한다

destory-method를 사용할 수 있다. 

 

 

얘는 스프링에 연결하지않음

<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>

 

 

public class ExampleBean {

    public void cleanup() {
        // do some destruction work (like releasing pooled connections)
    }
}
 

 

앞의 정의는 다음 정의와 거의 동일한 효과를 가진다.

<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
public class AnotherExampleBean implements DisposableBean {

    @Override
    public void destroy() {
        // do some destruction work (like releasing pooled connections)
    }
}

 

**

<bean> 요소의 destroy-method 속성에 특별한(유추된) 값을 할당할 수 있다

이 값은 특정 빈 클래스에서 공개 닫기 또는 종료 메소드를 자동으로 감지하도록 Spring에 지시한다.

따라서 java.lang.AutoCloseable 또는 java.io.Closeable을 구현하는 모든 클래스가 일치한다.

 

이 동작을 적용하기 위해 <beans> 요소의 default-destroy-method 속성에 이 특수(추론된) 값을 설정할 수도 있다.

 

Default Initialization and Destroy Methods

https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-lifecycle-default-init-destroy-methods

 

Core Technologies

In the preceding scenario, using @Autowired works well and provides the desired modularity, but determining exactly where the autowired bean definitions are declared is still somewhat ambiguous. For example, as a developer looking at ServiceConfig, how do

docs.spring.io

 

728x90