首页 > 编程语言 > 详细

spring和springMVC的上下文

时间:2016-06-22 15:27:21      阅读:292      评论:0      收藏:0      [点我收藏+]

上下文可以替代注解, 但是注解更方便

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";
	}
}

  

spring和springMVC的上下文

原文:http://www.cnblogs.com/wujixing/p/5606964.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!