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。

猜你喜欢

转载自lynnwong.iteye.com/blog/2118756