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" />