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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值