您的位置:首页 > 编程语言 > Java开发

spring和springMVC的上下文

2016-06-22 14:21 706 查看
上下文可以替代注解, 但是注解更方便

package com.tgb.web.controller;

import javax.annotation.Resource;
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 {
//	@Resource(name="springService")
//	private ISpring springService;

@RequestMapping("/spring/get")
public String get(HttpServletRequest request){
//spring的上下文
WebApplicationContext ac1 = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
//springMVC的上下文
WebApplicationContext ac2 = RequestContextUtils.getWebApplicationContext(request);

//通过spring上下文拿到bean
//ISpring springService = (ISpring)ac1.getBean("springService");

//通过springMVC上下文拿到bean
ISpring springService = (ISpring)ac2.getBean("springService");
System.out.println(springService.get());
return "/success";
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: