zoukankan      html  css  js  c++  java
  • 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";
    	}
    }
    

      

  • 相关阅读:
    关闭ThinkPHP Runtime Log
    Robots协议的写法
    Getif工具获取 交换机的OID/Zabbix设置
    命令方式启动 vmware 虚拟机
    wireshark 抓包使用教程
    Sniffer 交换机端口抓包
    H3C 开启ssh/snmp
    Apache Superset BI
    洛谷5588 小猪佩奇爬树
    luogu5536 核心城市
  • 原文地址:https://www.cnblogs.com/wujixing/p/5606964.html
Copyright © 2011-2022 走看看