1.6.2 ApplicationContextAware과BeanNameAware
ApplicationContex
가 org.springframework.context.ApplicationContextAware
인터페이스를 구현하는 객체 인스턴스를 생성 할 때, 인스턴스는 해당 ApplicationContext
에 대한 참조와 함께 제공됩니다. 다음 목록은 ApplicationContextAware
인터페이스 의 정의를 보여줍니다 .
public interface ApplicationContextAware {
void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}
따라서
따라서 bean은 이를 생성 한 ApplicationContext
를 프로그래밍 방식으로 조작 할 수 있습니다.
ApplicationContext
인터페이스를 통해 또는이 인터페이스의 알려진 서브 클래스 (예 : ConfigurableApplicationContext
.) 한 가지 용도는 다른 bean을 프로그래밍 방식으로 검색하는 것입니다. 때때로 이 기능은 유용합니다. 그러나 일반적으로 Spring에 코드를 연결하고 공동 작업자가 Bean에 속성으로 제공되는 Inversion of Control 스타일을 따르지 않으므로 이를 피하십시오. ApplicationContext
의 다른 메소드는 파일 자원에 대한 액세스, 응용 프로그램 이벤트 공개 및 MessageSource
액세스를 제공합니다. 이러한 추가 기능에서 설명> ApplicationContext
의 추가 기능.
Spring 2.5에서, autowiring은에 대한 참조를 얻기위한 또 다른 대안 ApplicationContext
이다. "전통적" constructor
및 byType
autowiring 모드 ( Autowiring Collaborators에 설명되어 있음 )는 ApplicationContext
각각 생성자 인수 또는 setter 메서드 매개 변수에 대한 유형의 종속성을 제공 할 수 있습니다 . autowire 필드 및 다중 매개 변수 메소드를 포함한 더 많은 유연성을 위해 새로운 주석 기반 자동 와이어 링 기능을 사용하십시오. 그렇게 ApplicationContext
하면 필드는 ApplicationContext
해당 필드, 생성자 또는 메서드가 @Autowired
주석을 전달하는 경우 해당 형식 을 예상하는 필드, 생성자 인수 또는 메서드 매개 변수로 자동 작성됩니다. 자세한 내용은 @Autowired
사용 참조하십시오 .
ApplicationContext
가 org.springframework.beans.factory.BeanNameAware
를 구현하는 클래스를 생성 할 때, 클래스에는 관련 객체 정의에 정의 된 이름에 대한 참조가 제공됩니다. 다음 목록은 BeanNameAware 인터페이스의 정의를 보여줍니다.
public interface BeanNameAware {
void setBeanName(String name) throws BeansException;
}
콜백은 일반적인 bean 프라퍼티의 활성화 이후지만 같은 초기화 콜백(InitializingBean
, afterPropertiesSet
또는 사용자 정의 초기화-방법.) 전에 호출
Last updated
Was this helpful?