【SpringBoot笔记12】实现xxxAware接口

参考:官方文档

1 Aware接口

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

1.1 ApplicationContextAware 和 BeanNameAware

1.1.1 ApplicationContextAware接口

当一个bean实现了org.springframework.context.ApplicationContextAware接口,那么这个实力bean就会被注入ApplicationContext对象,下面是这个接口的定义:

public interface ApplicationContextAware {

    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}

使用它的一个例子:

@Component
public class BeanA implements ApplicationContextAware {
    
    private ApplicationContext ctx;
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.ctx = applicationContext;
    }
    
    // ...
}

Spring 2.5开始,可以通过@Autowired自动注入ApplicationContext对象:

@Component
public class BeanA {
    
    @Autowired
    private ApplicationContext applicationContext;
    
    // ...
}

1.1.2 BeanNameAware接口

bean 的内部,它是不知道容器给自己取的id是什么。当一个bean实现了org.springframework.beans.factory.BeanNameAware接口,就可以在创建这个bean的时候将id注入进来。下面是该接口的定义:

public interface BeanNameAware {

    void setBeanName(String name) throws BeansException;
}

这些回调是在属性填充完毕之后,在初始化回调之前调用。

1.2 其它的Aware接口

Spring还提供了一些很重要的Aware接口,这些接口的命名规则就是根据需要依赖的属性来命名的:

接口注入的属性Explained in…
ApplicationContextAwareDeclaring ApplicationContext.ApplicationContextAware and BeanNameAware
ApplicationEventPublisherAwareEvent publisher of the enclosing ApplicationContext.Additional Capabilities of the ApplicationContext
BeanClassLoaderAwareClass loader used to load the bean classes.Instantiating Beans
BeanFactoryAwareDeclaring BeanFactory.ApplicationContextAware and BeanNameAware
BeanNameAwareName of the declaring bean.ApplicationContextAware and BeanNameAware
BootstrapContextAwareResource adapter BootstrapContextthe container runs in. Typically available only in JCA aware ApplicationContextinstances.JCA CCI
LoadTimeWeaverAwareDefined weaver for processing class definition at load time.Load-time Weaving with AspectJ in the Spring Framework
MessageSourceAwareConfigured strategy for resolving messages (with support for parametrization and internationalization).Additional Capabilities of the ApplicationContext
NotificationPublisherAwareSpring JMX notification publisher.Notifications
ResourceLoaderAwareConfigured loader for low-level access to resources.Resources
ServletConfigAwareCurrent ServletConfig the container runs in. Valid only in a web-aware Spring ApplicationContext.Spring MVC
ServletContextAwareCurrent ServletContext the container runs in. Valid only in a web-aware Spring ApplicationContext.Spring MVC
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值