zoukankan      html  css  js  c++  java
  • 获取Spring容器中的Bean

    摘要

    SpringMVC框架开发中可能会在Filter或Servlet中用到spring容器中注册的java bean 对象,获得容器中的java bean对象有如下方法

    Spring中的ApplicationContexts可以被限制在不同的作用域。在web框架中,每个DispatcherServlet有它自己的WebApplicationContext,它包含了DispatcherServlet配置所需要的bean。DispatcherServlet 使用的缺省BeanFactory是XmlBeanFactory,并且DispatcherServlet在初始化时会在你的web应用的WEB-INF目录下寻找[servlet-name]-servlet.xml文件。DispatcherServlet使用的缺省值可以使用servlet初始化参数修改,

    WebApplicationContext仅仅是一个拥有web应用必要功能的普通ApplicationContext。它和一个标准的ApplicationContext的不同之处在于它能够解析主题,并且它知道和那个servlet关联(通过到ServletContext的连接)。WebApplicationContext被绑定在ServletContext上,当你需要的时候,可以使用RequestContextUtils找到WebApplicationContext。

    Spring的DispatcherServlet有一组特殊的bean,用来处理请求和显示相应的视图。这些bean包含在Spring的框架里,(可选择)可以在WebApplicationContext中配置,配置方式就象配置其它bean的方式一样。这些bean中的每一个都在下面被详细描述。待一会儿,我们就会提到它们,但这里仅仅是让你知道它们的存在以便我们继续讨论DispatcherServlet。对大多数bean,都提供了缺省值,所有你不必要担心它们的值。

    servletContext 是web应用程序的大环境,用于存储整个web应用程序级别的对象,不知道这样说法是否对.

    ApplicationContext,WebApplicationContext 是Spring的BeanFactory,从名字中就可以知道区别拉,一个是支持web特性的BeanFactory。

    Spring获取WebApplicationContext与ApplicationContext的几种方法:

    方法一:在初始化时保存ApplicationContext对象 
    代码: 
    ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
    ac.getBean("beanId"); 
    说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。 

    方法二:通过Spring提供的工具类获取ApplicationContext对象 
    代码: 
    import org.springframework.web.context.support.WebApplicationContextUtils; 
    ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
    ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
    ac1.getBean("beanId"); 
    ac2.getBean("beanId"); 
    说明: 
    这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。 

    上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。 

    其中 servletContext sc 可以具体 换成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext(); 另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出 WebApplicationContext 对象: WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

    方法三:继承自抽象类ApplicationObjectSupport 
    说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。
    Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。

    方法四:继承自抽象类WebApplicationObjectSupport 
    说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext 

    方法五:实现接口ApplicationContextAware 
    说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。
    Spring初始化时,会通过该方法将ApplicationContext对象注入。 


    在web应用中一般用ContextLoaderListener加载webapplication,如果需要在action之外或者control类之外获取webapplication思路之一是,单独写个类放在static变量中

    如下:

    1.

    public class SpringContextUtil implements ApplicationContextAware{
     
     private static ApplicationContext applicationContext;//Spring应用上下文环境
     @Override
     public void setApplicationContext(ApplicationContext applicationcontext)
       throws BeansException {
      // TODO Auto-generated method stub
      SpringContextUtil.applicationContext=applicationcontext;
     }
      public static Object getBean(String name){
      return applicationContext.getBean(name);
     }
    }

    //applicationContext.xml 配置
    // <bean id="SpringContextUtil" class="com.zjd.util.SpringContextUtil"/>

    SpringContextUtil.getBean("bean id");

    2.

    WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);

    ctx.getBean("bean id");

  • 相关阅读:
    php多态
    ssl certificate problem: self signed certificate in certificate chain
    test plugin
    open specific port on ubuntu
    junit vs testng
    jersey rest service
    toast master
    use curl to test java webservice
    update folder access
    elk
  • 原文地址:https://www.cnblogs.com/huqianliang/p/5653301.html
Copyright © 2011-2022 走看看