实现了ApplicationContextAware接口

问题:实现了ApplicationContextAware接口后在普通类中还是获取不到bean

首先来看一下对应的例子:

TestApplicationContextAware.java

@Component
public class TestApplicationContextAware implements ApplicationContextAware {

    private static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context=applicationContext;
    }
    public static ApplicationContext getApplicationContext(){
        return context;
    }

    public static Object getBean(String name){
        //return context.getBean(name); //为什么这样写会报空指针错误
        return getApplicationContext().getBean(name);
    }

    public static <T> T getBean(Class<T> requiredType) throws BeansException{
        return getApplicationContext().getBean(requiredType);
    }
    public void show() {
        System.out.println("Demo3: " + context);
    }

}

WebConfig.java

@Configuration
public class WebConfig {
    @Bean
    public User getUser(){
        return new User(5,"48");
    }
}

User.java

public class User {
    private Integer age;
    private String name;
    User(Integer _age,String _name){
        this.age=_age;
        this.name=_name;
    }
}

Client.java

public class Client {
    public static void main(String[] args) {
        //先需要将类纳入IOC容器管理,下面就是创建IOC容器.applicationContext 是一个应用上下文
        AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext(TestApplicationContextAware.class);
        //ConfigurableApplicationContext 是另外一个应用上下文
        ConfigurableApplicationContext a=new AnnotationConfigApplicationContext(WebConfig.class);
        System.out.println(applicationContext.getBean(TestApplicationContextAware.class));
        
        TestApplicationContextAware testApplicationContextAware= (TestApplicationContextAware) TestApplicationContextAware.getBean("testApplicationContextAware");
        //2. User user = (User) TestApplicationContextAware.getBean("getUser");
        System.out.println(testApplicationContextAware.hashCode());
    }
}

通过TestApplicationContextAware获取getUser这个bean时会提示: No bean named ‘getUser’ available。

原因分析

实现了ApplicationContextAware接口的类只能获得当前上下文中的bean,也就是applicationContext这个上下文。

String[] beans = applicationContext.getBeanDefinitionNames();
Arrays.sort(beans);
for (String bean : beans) {
	System.out.println("applicationContext上下文管理的bean: "+bean);
}

输出:
在这里插入图片描述
通过上面的输出可以看到applicationContext这个上下文并没有管理getUser这个bean,所以获取不到对应的bean,只能获取到testApplicationContextAware这个bean。

记录一下自己踩坑的过程

第一次自己直接通过TestApplicationContextAware.getBean(“getUser”);去获取bean,TestApplicationContextAware.java类报错context为null,当时觉得自己已经使用注解了,类肯定也被Spring管理了。找了很久的错误,突然发现自己都没有创建IOC容器去管理bean,仅仅通过注解怎么就能让bean被Spring管理了呢。
所以后面先创建了Spring上下文管理Bean,但是通过TestApplicationContextAware.getBean(“getUser”)还是获取不到实例,经过思考要想通过IOC容器获得bean的前提该bean被IOC容器管理,但是getUser这个bean并不在applicationContext这个上下文的管控中,当然是获取不到该实例的。

学习进阶:

setApplicationContext是Spring框架预留的一个关键的钩子方法,spring详细加载全过程如下:
调用 BeanNameAware 的 setBeanName 方法
调用 BeanFactoryAware 的 setBeanFactory 方法
调用 ApplicationContextAware 的 setApplicationContext
调用 InitializingBean 的 afterPropertiesSet 或者没有实现这个接口,但指定了@Bean(initMethod=“不加括号的方法名”),会执行这个方法
调用 BeanPostProcessor 的 postProcessBeforeInitialization 方法
调用 BeanPostProcessor 的 postProcessAfterInitialization 方法
Bean 初始化完成,可以被使用
容器关闭前,调用 DisposableBean 的 destroy 方法
加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的setApplicationContext,自动的将ApplicationContext注入进来。

在ApplicationContextAware的实现类中,就可以通过这个上下文环境对象得到Spring容器中的Bean。
参考文献:https://zhuanlan.zhihu.com/p/170542769
ApplicationContextAware执行时机:https://www.cnblogs.com/winclpt/articles/7428229.html
一步一步看ApplicationContextAware执行顺序步骤: https://blog.csdn.net/w605283073/article/details/125814360

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值