• spring和springMVC的上下文


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

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

      

  • 相关阅读:
    测试sql语句性能,提高执行效率
    js积累
    如何提高AJAX客户端响应速度
    视频代码
    网页视频播放器收集
    WinForm软件开机自动启动详细方法
    JS时间格式化函数
    (转)CSS+DIV float 定位
    CSS+DIV 布局三种定位方式
    CSS+DIV布局初练—DIV元素必须成对出现?
  • 原文地址:https://www.cnblogs.com/wujixing/p/5606964.html
Copyright © 2020-2023  润新知