zoukankan      html  css  js  c++  java
  • 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。

  • 相关阅读:
    AJAX请求 $.post方法的使用
    通过jQuery Ajax使用FormData对象上传文件
    iframe跨域访问
    js编码解码
    ajax头像上传
    无边框窗体和用户控件以及权限
    只开启一个窗体和进程以及多线程的操作
    通过一个窗体操作另一个窗体
    流和打印控件用法
    listview和简单的记事本操作
  • 原文地址:https://www.cnblogs.com/qiushizhu/p/3983933.html
Copyright © 2011-2022 走看看