> For the complete documentation index, see [llms.txt](https://develop-footprint.gitbook.io/o/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://develop-footprint.gitbook.io/o/spring/spring-core/bean/aware.md).

# Aware

* Spring은 Aware라는 인터페이스를 제공한다.
* Aware 인터페이스를 구현하는 객체 인스턴스를 생성하면 특정 클래스에 대한 참조가 함께 제공된다.
* 특정 클래스에 대한 참조를 얻어 타입 캐스팅하거나 메서드를 호출하는 등 직접 여러 기능을 사용할 수 있다.
* 하지만 이 방식을 사용하면 스프링 컨텍스트와 강하게 결합된 코드가 되며 IoC를 따르지 않기 때문에 일반적으로는 사용하지 않는 것이 좋다고 한다.
* Aware를 구현하는 대신 Autowire하여 참조를 얻는 것이 더 권장된다. (생성자 주입 / Setter 주입 / 필드 주입)
* Aware의 종류는 아래와 같다.

| Name                           | Injected Dependency                                                                                      | Explained in…​                                                                                                                                                                                                                                                                                                 |
| ------------------------------ | -------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ApplicationContextAware        | Declaring ApplicationContext.                                                                            | <https://docs.spring.io/spring-framework/reference/core/beans/factory-nature.html#beans-factory-awarehttps://docs.spring.io/spring-framework/reference/core/beans/factory-nature.html#beans-factory-awarehttps://docs.spring.io/spring-framework/reference/core/beans/factory-nature.html#beans-factory-aware> |
| ApplicationEventPublisherAware | Event publisher of the enclosing ApplicationContext.                                                     | <https://docs.spring.io/spring-framework/reference/core/beans/context-introduction.htmlhttps://docs.spring.io/spring-framework/reference/core/beans/context-introduction.html>                                                                                                                                 |
| BeanClassLoaderAware           | Class loader used to load the bean classes.                                                              | <https://docs.spring.io/spring-framework/reference/core/beans/definition.html#beans-factory-class>                                                                                                                                                                                                             |
| BeanFactoryAware               | Declaring BeanFactory.                                                                                   | <https://docs.spring.io/spring-framework/reference/core/beans/beanfactory.htmlhttps://docs.spring.io/spring-framework/reference/core/beans/beanfactory.htmlhttps://docs.spring.io/spring-framework/reference/core/beans/beanfactory.html>                                                                      |
| BeanNameAware                  | Name of the declaring bean.                                                                              | <https://docs.spring.io/spring-framework/reference/core/beans/factory-nature.html#beans-factory-awarehttps://docs.spring.io/spring-framework/reference/core/beans/factory-nature.html#beans-factory-awarehttps://docs.spring.io/spring-framework/reference/core/beans/factory-nature.html#beans-factory-aware> |
| LoadTimeWeaverAware            | Defined weaver for processing class definition at load time.                                             | <https://docs.spring.io/spring-framework/reference/core/aop/using-aspectj.html#aop-aj-ltw>                                                                                                                                                                                                                     |
| MessageSourceAware             | Configured strategy for resolving messages (with support for parameterization and internationalization). | <https://docs.spring.io/spring-framework/reference/core/beans/context-introduction.htmlhttps://docs.spring.io/spring-framework/reference/core/beans/context-introduction.html>                                                                                                                                 |
| NotificationPublisherAware     | Spring JMX notification publisher.                                                                       | <https://docs.spring.io/spring-framework/reference/integration/jmx/notifications.html>                                                                                                                                                                                                                         |
| ResourceLoaderAware            | Configured loader for low-level access to resources.                                                     | <https://docs.spring.io/spring-framework/reference/web/webflux-webclient/client-builder.html#webflux-client-builder-reactor-resources>                                                                                                                                                                         |
| ServletConfigAware             | Current ServletConfig the container runs in. Valid only in a web-aware Spring ApplicationContext.        | <https://docs.spring.io/spring-framework/reference/web/webmvc.html#mvc>                                                                                                                                                                                                                                        |
| ServletContextAware            | Current ServletContext the container runs in. Valid only in a web-aware Spring ApplicationContext.       | <https://docs.spring.io/spring-framework/reference/web/webmvc.html#mvc>                                                                                                                                                                                                                                        |

## 사용 사례

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

```java
public class MongoIndexedSessionRepository
		implements FindByIndexNameSessionRepository<MongoSession>, ApplicationEventPublisherAware, InitializingBean {

	// ...

	private ApplicationEventPublisher eventPublisher;

	@Override
	public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) {
		this.eventPublisher = eventPublisher;
	}

	// ...

}
```
