Aware

  • Spring은 Aware라는 인터페이스를 제공한다.

  • Aware 인터페이스를 구현하는 객체 인스턴스를 생성하면 특정 클래스에 대한 참조가 함께 제공된다.

  • 특정 클래스에 대한 참조를 얻어 타입 캐스팅하거나 메서드를 호출하는 등 직접 여러 기능을 사용할 수 있다.

  • 하지만 이 방식을 사용하면 스프링 컨텍스트와 강하게 결합된 코드가 되며 IoC를 따르지 않기 때문에 일반적으로는 사용하지 않는 것이 좋다고 한다.

  • Aware를 구현하는 대신 Autowire하여 참조를 얻는 것이 더 권장된다. (생성자 주입 / Setter 주입 / 필드 주입)

  • Aware의 종류는 아래와 같다.

Name
Injected Dependency
Explained in…​

LoadTimeWeaverAware

Defined weaver for processing class definition at load time.

ServletConfigAware

Current ServletConfig the container runs in. Valid only in a web-aware Spring ApplicationContext.

ServletContextAware

Current ServletContext the container runs in. Valid only in a web-aware Spring ApplicationContext.

사용 사례

  • ApplicationEventPublisherAware를 사용하여 eventPublisher 참조를 MongoIndexedSessionRepository 객체에 주입할 수 있다. (3.2.1 버전 기준)

Last updated