spring 获取bean的几种方法

spring 获取bean的几种方法

  1. 实现BeanFactoryAware

    public class ServiceLocator implements BeanFactoryAware {

        private static BeanFactory beanFactory;
    
        @Override
        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
            ServiceLocator.beanFactory = beanFactory;
        }
    
        /**
         * 根据提供的bean名称得到相应的服务类
         * 
         * @param servName
         * bean名称
         */
        public static Object getService(String servName) {
            return beanFactory.getBean(servName);
        }
    
    }
    

    spring配置文件

    <bean id="serviceLocator" class="com.taobao.appcenter.common.ServiceLocator" lazy-init="false"/>
    
  2. 实现ApplicationContextAware

    public class SpringContextsUtil implements ApplicationContextAware {
    
        private static ApplicationContext applicationContext;    //Spring context   
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            SpringContextsUtil.applicationContext = applicationContext;
        }
    
        /**
          * @return ApplicationContext
          */
          public static ApplicationContext getApplicationContext() {
            return applicationContext;
          }
    
          /**
          * 获取对象  
          * @param name
          * @return Object 一个以所给名字注册的bean的实例
          * @throws BeansException
          */
          public static Object getBean(String name) throws BeansException {
            return applicationContext.getBean(name);
          }
    }
    

    spring xml配置文件

    <bean id="springContext" class="com.taobao.appcenter.common.SpringContext" lazy-init="false"/>
    
  3. 通过servlet 或listener设置spring的ApplicationContext,以便后来引用,这个是针对web 工程的

        public class SpringContext {
        private static ApplicationContext applicationContext; // Spring应用上下文环境
    
        public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            SpringContext.applicationContext = applicationContext;
        }
    
        /**
         * @return ApplicationContext
         */
        public static ApplicationContext getApplicationContext() {
            return applicationContext;
        }
    
        public static Object getBean(String name) throws BeansException {
            return applicationContext.getBean(name);
        }
    }
    

    上面的SpingContext谁没实现任何接口的,不是回调的方式。而是在listener或者servlet中set进来

    public class SpringContextLoaderListener extends ContextLoaderListener {
    
    
        public void contextInitialized(ServletContextEvent event){
            super.contextInitialized(event);
            SpringContext.setApplicationContext(WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()));
        }
    
    
    }
    

    替换web.xml文件的listener为上面的文件

    <listener>
        <listener-class>com.taobao.appcenter.common.SpringContextLoaderListener</listener-class>
    </listener>
    

    servlet的代码如下

    public class SpringContextLoaderServlet extends ContextLoaderServlet {
     private ContextLoader contextLoader;
        public void init() throws ServletException {
            this.contextLoader = createContextLoader();
            SpringContext.setApplicationContext(this.contextLoader.initWebApplicationContext(getServletContext()));
        }
    }
    

    然后配置web.xml中的servlt和mapping即可。

  4. 调用

    由于使用的都是静态函数,使用getBean(Stirng name)或者getService(String serviceName)就可以获得xml配置的业务自己bean。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值