Spring进阶篇(2)-ApplicationContextAware(动态获取容器中Bean)

JAVA && Spring && SpringBoot2.x — 学习目录

在Spring中,我们遇到最多的就是使用注解、xml配置的形式来转配Bean。

但是在某些场景下,即需要将对象作为Bean交给Spring管理,有需要在代码中即时地获取Bean,那么使用注解的形式就不太能满足需求。

1. ApplicationContextAware接口的作用

当一个类实现了这个接口之后,这个类就可以方便的获取ApplicationContext(应用上下文)中所有的bean。

2. 怎么使用这个接口

实现ApplicationContextAware接口。重写或继承里面的方法。
主要可以实现的功能:

  1. 根据byType获取byName获取Bean对象;
  2. 根据byName判断Bean对象是否存在;
  3. 根据bean对象的name获取type;
  4. 根据bean对象的type获取names;
@Component
public class SpringUtil implements ApplicationContextAware {

    //spring应用的上下文环境
    private static ApplicationContext applicationContext=null;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //实现ApplicationContextAware接口的回调方法,设置上下文环境
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    //byType获取对象
    public static <T> T getBean(Class<T> requiredType){
        return applicationContext.getBean(requiredType);
    }
    //byName+byType获取对象
    public static <T> T getBean(String name,Class<T> requiredType){
        return applicationContext.getBean(name,requiredType);
    }
    //byName获取对象
    public static Object getBean(String name){
        return applicationContext.getBean(name);
    }
    //byName判读对象是否存在
    public static  boolean contains(String name){
        return applicationContext.containsBean(name);
    }
    //判断对象是否是单例
    public static boolean isSignleton(String name){
        return applicationContext.isSingleton(name);
    }
    //判断注册对象的类型
    public static Class<?> getType(String name){
        return applicationContext.getType(name);
    }
    //返回bean的别名
    public static String[] getAliases(String name){
        return applicationContext.getAliases(name);
    }

    //根据类型获取bean的名字
    public String[] getBeanNamesForType(Class<?> clazz){
        return applicationContext.getBeanNamesForType(clazz);
    }

}

需要注意的是,需要将这个类放入Spring容器中管理。即使用@Component注解。

3. 单元测试中使用这个接口

需要注意的是:name不是Bean对象的全限定名,而是Spring容器中的Bean名。

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestSpringUtil {

    private static String COMMON_CLASS="com.tellme.job.TestJob";
    @Test
    public void testSpringUtilOfGetBean(){
        try {
            Class<?> clazz=Class.forName(COMMON_CLASS);
            TestJob testJob= (TestJob) SpringUtil.getBean(clazz);
            testJob.executeInternal();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testSpringUtilOfContains(){
        boolean bean = SpringUtil.containsBean("testJob");
        System.out.println(bean);
    }

    @Test
    public void testGetBeanNamesForType(){
        try {
            String[] beanNamesForType = SpringUtil.getBeanNamesForType(Class.forName(COMMON_CLASS));
            for(String names: beanNamesForType){
                System.out.println(names);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容