springMVC(13) springMVC和spring上下文关系

springmvc上下文继承于spring,也就是springmvc的上下文可访问spring上下文,在springmvc的上下文中可取得spring bean.

SpringController

package com.xdy.controller.annotation;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.support.RequestContextUtils;

@Controller
public class SpringController {
	@RequestMapping("/spring/get")
	public String get(HttpServletRequest request) {
		// spring上下文
		WebApplicationContext ac1 = WebApplicationContextUtils
				.getWebApplicationContext(request.getSession()
						.getServletContext());

		//springMVC的上下文
		WebApplicationContext ac2 = RequestContextUtils
				.getWebApplicationContext(request);
		
//		ISpring springManager = (ISpring) ac1.getBean("springManager");
		ISpring springManager = (ISpring) ac2.getBean("springManager");
		springManager.get();
		return "/success";
	}
}

spring配置小技巧:import标签

团队开发时,各自维护自己的spring配置文件,这时就可以使用一个公用的spring来进行导入就可以了。

<import
		resource="classpath*:com/xdy/controller/annotation/springAnnotation-import.xml" />


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值