• Spring中如何获取JavaBean呢?


    转自:

    Spring中如何获取JavaBean呢?

    下文笔者讲述Spring中获取Bean的方法分享,如下所示

    我们都知道,Spring中可使用@Resource/@Autowired
    即可获取Spring对象,但是有些特殊的场景,我们只能通过另外的方式获取bean
    下文笔者将一一道来,如下所示
    

    例:
    SpringBoot获取Bean的方法

    方式1:SpringBoot 获取ApplicationContext

        @Autowired  
        ApplicationContext context; 
     

    方式2:继承ApplicationContextAware 来获取Bean

    创建一个类继承ApplicationContextAware

    package com.java265;
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    public class ApplicationContextUtil implements ApplicationContextAware {
        private static ApplicationContext applicationContext;
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
            this.applicationContext = applicationContext;
        }
        public static ApplicationContext getContext() {
            return applicationContext;
        }
        public static  T getBean(String name) {
            if (applicationContext == null) {
                return null;
            }
            return (T) applicationContext.getBean(name);
        }
    }
    

    将ApplicationContextAware 配置进application-context.xml

    
    

    方式3:获取bean的使用场景

    TestBean testBean = ApplicationContextUtil.getBean("bean名称");
     

    方式4:使用ContextLoader 获取bean

    ApplicationContext app = ContextLoader.getCurrentWebApplicationContext();
    TestBean testBean = app.getBean("testBean", TestBean.class);
    

    方式5:继承 BeanFactoryAware 获取bean

    创建一个类继承BeanFactoryAware

    package com.java265;
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.BeanFactoryAware;
     
    public class SpringBeanFactory implements BeanFactoryAware {
        private static BeanFactory beanFactory;
        @Override
        public void setBeanFactory(BeanFactory factory) throws BeansException {
            this.beanFactory = factory;
        }
     
        /**
         * 根据beanName名字取得bean
         * @param name
         * @param 
         * @return
         */
        public static  T getBean(String name) {
            if (beanFactory == null) {
                return null;
            }
            return (T) beanFactory.getBean(name);
        }
    }
    
    SpringBeanFactory 配置进application-context.xml
    
    
    在需要的地方获取bean
    
    TestBean testBean = SpringBeanFactory.getBean("bean名称");
  • 相关阅读:
    element表单验证常用的几种规则
    Memcached 教程之 PHP 连接 Memcached 服务(十)
    JavaScript防抖和节流的使用及区别
    多路由复用页面组件问题
    Meta Starburst体验:HDR效果惊人,但体积重量劝退
    大二暑假 + 大三上
    SpringCloud系列——Feign+Ribbon配置实战篇
    【python】Algorithm
    react-native在模拟器调试时不能连接开发服务器:Could not connect to development server.
    C++中的深拷贝和浅拷贝介绍
  • 原文地址:https://blog.csdn.net/qq_25073223/article/details/127547028