Aware接口

Posted by KANG's BLOG on Tuesday, March 15, 2022

Aware接口

aware

adj. 知道; 意识到; 明白; 察觉到; 发觉; 发现; 对…有兴趣的; 有…意识的;

当bean实现了对应的Aware接口,BeanFactory 就会在产生这个bean的时候根据对应的Aware接口,给这个bean注入相应的属性,这样bean就能够获取外界资源的引用了.

1. BeanNameAware

public interface BeanNameAware {
    void setBeanName(String name) throws BeansException;
}

继承该接口,将实现setBeanName方法,bean的实例会被注入该bean的名称。

2. ApplicationContextAware

public interface ApplicationContextAware {
    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}

获取spring上下文

3. BeanFactoryAware

获取beanFactory

4. 其他

接口 注入的属性 Explained 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. ApplicationContextAware and BeanNameAware
BeanNameAware Name of the declaring bean. ApplicationContextAware and BeanNameAware
BootstrapContextAware Resource adapter BootstrapContextthe container runs in. Typically available only in JCA aware ApplicationContextinstances. JCA CCI
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 parametrization 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