如何让不在Spring容器中的类可以获取到容器里的Bean

1、创建一个上下文类并且交由Spring容器管理:实现ApplicationContextAware,ApplicationContextAware里的逻辑是,在Spring容器启动时,查找哪些类实现了它,然后会自动调用实现类里的setApplicationContext方法

2、定义好上图中的出口方法,static的getBean(name或者Class)

3、然后就直接可以当单例模式的对象,在别的类里使用取Bean了 

你可能感兴趣的:(如何让不在Spring容器中的类可以获取到容器里的Bean)