zoukankan      html  css  js  c++  java
  • spring mvc在Controller中获取ApplicationContext

    spring mvc在Controller中获取ApplicationContext

    web.xml中进行正常的beans.xml和spring-mvc.xml的配置;

    需要在beans.xml中进行需要加载的类的配置;不能在spring-mvc.xml中配置;

    beans.xml中的配置:

        <bean id="deployMode" class="java.lang.String">
            <constructor-arg>
                <value>development</value><!-- production -->
            </constructor-arg>
        </bean>

    Controller中三种获取方式:

        @RequestMapping(value = "/deploymode")
        @ResponseBody
        public String getDeployMode(HttpServletRequest request) {
            // 1, ContextLoader.getCurrentWebApplicationContext
            // WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
            // String deploy = context.getBean("deployMode", String.class);
            // return deploy;
            // 2,servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)
            ServletContext servletContext = request.getSession().getServletContext();
            WebApplicationContext context = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
            String deploy = context.getBean("deployMode2", String.class);
            return deploy;
            // 3,RequestContextUtils.getWebApplicationContext(request);
            // WebApplicationContext context = RequestContextUtils.getWebApplicationContext(request);
            // String deploy = context.getBean("deployMode", String.class);
            // return deploy;
        }

    参考:

    http://www.07net01.com/linux/springMVCzaiControllerzhonghuoquWebApplicationContext_656388_1381902854.html

    http://zhidao.baidu.com/link?url=ThsMgUqmhDMzlI7MZrB0346vSm1y1e4mNOIzbmSg8AYyAZCwygiSIE0EHHKd3R-rI_lH1M6BEDen68ARPWGXOFkBM9xldUWAFcToaK9edyq

  • 相关阅读:
    Bootstrap 2.2.2 的新特性
    Apache POI 3.9 发布,性能显著提升
    SQL Relay 0.48 发布,数据库中继器
    ProjectForge 4.2.0 发布,项目管理系统
    红帽企业 Linux 发布 6.4 Beta 版本
    红薯 快速的 MySQL 本地和远程密码破解
    MariaDB 宣布成立基金会
    Percona XtraBackup 2.0.4 发布
    Rocks 6.1 发布,光盘机群解决方案
    精通Servlet研究,HttpServlet的实现追究
  • 原文地址:https://www.cnblogs.com/stono/p/4938674.html
Copyright © 2011-2022 走看看