spring mvc在Controller中獲取ApplicationContexthtml
web.xml中進行正常的beans.xml和spring-mvc.xml的配置;java
須要在beans.xml中進行須要加載的類的配置;不能在spring-mvc.xml中配置;linux
beans.xml中的配置:web
<bean id="deployMode" class="java.lang.String"> <constructor-arg> <value>development</value><!-- production --> </constructor-arg> </bean>
Controller中三種獲取方式:spring
@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; }
參考:spring-mvc
http://www.07net01.com/linux/springMVCzaiControllerzhonghuoquWebApplicationContext_656388_1381902854.htmlmvc
http://zhidao.baidu.com/link?url=ThsMgUqmhDMzlI7MZrB0346vSm1y1e4mNOIzbmSg8AYyAZCwygiSIE0EHHKd3R-rI_lH1M6BEDen68ARPWGXOFkBM9xldUWAFcToaK9edyqapp