《Java Spring框架》SpringBoot 手工获取Spring容器中的Bean对象

前言

为了排查问题等一些原因需要查看Spring容器中Bean的使用情况,所以程序运行的过程中需要获取Bean的信息。

代码演示

@Component
public class BeanUtil implements ApplicationContextAware {


    private ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}

能够获取到ApplicationContext 那就好办了, 我们一起看一下这个对象的属性和方法。

总结

这样就可以更加方便的了解Spring的代码

posted @ 2021-04-20 20:01  加速丨世界  阅读(432)  评论(0编辑  收藏  举报