본문 바로가기

Web/spring

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

728x90

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

 

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.2. ApplicationContextAware and BeanNameAware

ApplicationContext가 org.springframework.context.ApplicationContextAware 인터페이스를 구현하는 객체 인스턴스를 생성할 때 instance는 해당 ApplicationContext에 대한 참조와 함께 제공된다.

 

public interface ApplicationContextAware {

    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}
 

 

따라서 Bean은 ApplicationContext 인터페이스를 통하거나 알려진 하위 클래스 (예시 ConfigurableApplicationContext) 에 대한 참조를 castiing 하여 자기를 생성한 ApplicationContext 를 조작할 수 있다

이 용도는 bean의 프로그래밍 방ㅎ식 검색이다. 일반적으로 코드를 Spring에 연결하고 속성으로 제공되는 Inversion of Control 스타일을 따르지 않기 때문에 피해야 한다

 

ApplicationContext의 다른 메서드는 파일 리소스에 대한 액세스, 애플리케이션 이벤트 게시 및 MessageSource 액세스를 제공한다.

이러한 추가 기능은 ApplicationContext의 추가 기능에 설명되어 있다.

Autowiring은 ApplicationContext에 대한 참조를 얻기 위한 또 다른 대안이다다.

전통적인 생성자 및 byType 자동 연결 모드(Autowiring Collaborators에서 설명한 대로)는 각각 생성자 인수 또는 setter 메서드 매개 변수에 대해 ApplicationContext 유형의 종속성을 제공할 수 있다.

필드 및 여러 매개 변수 메서드를 자동 연결하는 기능을 포함하여 유연성을 높이려면 주석 기반 자동 연결 기능을 사용하면 된다. 그렇게 하면 해당 필드, 생성자 또는 메서드에 @Autowired 주석이 있는 경우 ApplicationContext는 ApplicationContext 유형을 예상하는 필드, 생성자 인수 또는 메서드 매개 변수에 자동 연결된다.

https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-autowired-annotation

 

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

 

 ApplicationContext가 org.springframework.beans.factory.BeanNameAware 인터페이스를 구현하는 클래스를 생성할 때, 클래스는 관련 객체 정의에 정의된 이름에 대한 참조를 제공받는다.

 

BeanNameAware 인터페이스의 정의

public interface BeanNameAware {

    void setBeanName(String name) throws BeansException;
}

 

콜백은 일반 bean 속성을 채운 후 InitializingBean.afterPropertiesSet() 또는 사용자 정의 init-method와 같은 초기화 콜백 전에 호출된다

 

 

 

1.6.3. Other Aware Interfaces

Besides ApplicationContextAware and BeanNameAware (discussed earlier), Spring offers a wide range of Aware callback interfaces that let beans indicate to the container that they require a certain infrastructure dependency. As a general rule, the name indicates the dependency type. The following table summarizes the most important Aware interfaces:

Table 4. Aware interfacesNameInjected DependencyExplained in…​

ApplicationContextAware Declaring ApplicationContext. ApplicationContextAware and BeanNameAware
ApplicationEventPublisherAware Event publisher of the enclosing ApplicationContext. Additional Capabilities of the ApplicationContext
BeanClassLoaderAware Class loader used to load the bean classes. Instantiating Beans
BeanFactoryAware Declaring BeanFactory. The BeanFactory API
BeanNameAware Name of the declaring bean. ApplicationContextAware and BeanNameAware
LoadTimeWeaverAware Defined weaver for processing class definition at load time. Load-time Weaving with AspectJ in the Spring Framework
MessageSourceAware Configured strategy for resolving messages (with support for parameterization and internationalization). Additional Capabilities of the ApplicationContext
NotificationPublisherAware Spring JMX notification publisher. Notifications
ResourceLoaderAware Configured loader for low-level access to resources. Resources
ServletConfigAware Current ServletConfig the container runs in. Valid only in a web-aware Spring ApplicationContext. Spring MVC
ServletContextAware Current ServletContext the container runs in. Valid only in a web-aware Spring ApplicationContext. Spring MVC

Note again that using these interfaces ties your code to the Spring API and does not follow the Inversion of Control style. As a result, we recommend them for infrastructure beans that require programmatic access to the container.

728x90