springmvc上下文继承于spring,

也就是springmvc的上下文可访问spring上下文,在springmvc的上下文中可取得spring bean.

spring上下文是spring启动的时候加载的spring的配置文件,目前用到的只是Spring bean 的上下文文件,

每一个dispatcherServlet上下文去对应一个spring上下文

SpringMVC只要写一个注解就可以拿到上下文,而不用去配置容器

测试一下两个的关系

1.SpringController

package com.tgb.web.controller.annotation;

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="springManager")
private ISpring springManager;*/ @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"); System.out.println(springManager.get());
return "/success";
}
}

2.防止每个人在开发的时候,都对一个spring.xml进行修改,则要写很多bean且会混乱

spring配置小技巧:import标签

团队开发时,各自维护自己的spring.xml 配置文件,这时就可以使用一个公用的spring.xml来进行导入就可以了。

<import  resource="classpath*:com/xdy/controller/annotation/springAnnotation-import.xml" /> 

spring配置小技巧:import标签
<import resource="classpath*:config/spring/spring_annotation-import.xml"/>
在团队开发时候,每个人都常去改动spring配置文件,不科学,使用这个技巧方便,在开发的时候,最好每个都有各自的配置文件了,每个人用一个配置文件,减少冲突
项目较大,有较多的bean时,可以将其分散到子文件中.
虽然spring还有自动扫描的功能,但我感觉也不怎么好,需要去扫描,影响性能;而且各个Bean分散在不同包中,不好配置.

15.SpringMVC和Spring上下文关系(为什么SpringMVC可以调用到Spring)的更多相关文章

  1. spring中的web上下文,spring上下文,springmvc上下文区别(超详细)

    web上下文(Servlet context),spring上下文(WebApplication Context),springmvc上下文(mlWebApplicationCont)之间区别. 上下 ...

  2. Spring和springMVC父子容器的关系

    部分转载自:https://www.cnblogs.com/ljdblog/p/7461854.html springMVC容器和Spring容器 为什么一定要在web.xml中配置spring的li ...

  3. callable与runable区别?switch char ?sql只查是否存在,sql复制表 ?反射 ? spring mvc 和spring 上下文区别?

    中化技术部  2018.4.16 1. callable 和 thread 区别 实现Callable接口的线程能返回执行结果,而Runable 不可以 . Callable 的call方法允许抛出异 ...

  4. (转载)Spring与SpringMVC父子容器的关系与初始化

    转自 https://blog.csdn.net/dhaiuda/article/details/80026354 Spring和SpringMVC的容器具有父子关系,Spring容器为父容器,Spr ...

  5. Web容器、Servlet容器、Spring容器、SpringMVC容器之间的关系

    以下内容为个人理解,如有误还请留言指出,不胜感激! Web容器 web容器(web服务器)主要有:Apache.IIS.Tomcat.Jetty.JBoss.webLogic等,而Tomcat.Jet ...

  6. web容器、sevlet容器、spring容器、springmvc容器之间的关系

    原文链接:http://www.cnblogs.com/jieerma666/p/10805966.html https://blog.csdn.net/zhanglf02/article/detai ...

  7. spring与springMVC的父子容器关系

    背景和概述 在spring与springMVC中通过IOC可以管理bean对象,有两个配置文件可以配置ioc spring的配置文件applicationContext.xmlspringMVC的配置 ...

  8. SSH(Struts,Spring,Hibernate)和SSM(SpringMVC,Spring,MyBatis)的区别

    SSH 通常指的是 Struts2 做前端控制器,Spring 管理各层的组件,Hibernate 负责持久化层. SSM 则指的是 SpringMVC 做前端控制器,Spring 管理各层的组件,M ...

  9. Java基础图解,JVM,线程,Spring,TCP,SpringMVC等开发体系图解

    Java基础图解,JVM,线程,Spring,TCP,SpringMVC等开发体系图解 1.Java虚拟机运行时数据区图 2. 堆的默认分配图 3.方法区结构图 4.对象的内存布局图 5.对象头的Ma ...

随机推荐

  1. realloc,malloc,calloc函数的区别

    from:http://www.cnblogs.com/BlueTzar/articles/1136549.html realloc,malloc,calloc的区别 三个函数的申明分别是: void ...

  2. CF459A Pashmak and Garden (水

    Pashmak and Garden Codeforces Round #261 (Div. 2) A. Pashmak and Garden time limit per test 1 second ...

  3. iOS: ARC & MRC下string内存管理策略探究

    ARC & MRC下string内存管理策略探究 前两天跟同事争论一个关于NSString执行copy操作以后是否会发生变化,两个人整了半天,最后写代码验证了一下,发现原来NSString操作 ...

  4. 调用WebService 实现在线双向翻译

    >先了解一下Web Service的基本概念: Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传 ...

  5. src 小心得

    关于src的引用,不要用相对路径,  ../   虽然省事,但是跳转页面时容易出错. 举个例子: 在web页面引用  D:\phpStudy\WWW\ueditor\utf8-php 这个文件夹下面  ...

  6. Python的Set和List的性能比较 + 两者之间的转换

    1.能用set 不用list ~$ python -m timeit -n 1000 "[x for x in range(1000) if x in range(500, 1500)]&q ...

  7. PHP array_multisort() 函数详解 及 二维数组排序(模拟数据表记录按字段排序)

    一.先看最简单的情况. 有两个数组: $arr1 = array(1, 9, 5); $arr2 = array(6, 2, 4); array_multisort($arr1, $arr2); pr ...

  8. ubutu之qq安装

    1.压缩包下载链接 http://pan.baidu.com/s/1nvlgsGh 2.安装教程(引用自百度经验) 如何在Ubuntu中安装QQ

  9. CSU 1337 搞笑版费马大定理(2013湖南省程序设计竞赛J题)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1337 解题报告:虽然x和y的范围都是10^8,但是如果a 是大于1000的话,那么a^3 ...

  10. BZOJ2002——[Hnoi2010]Bounce 弹飞绵羊

    1.题目大意:就是给一个动态的森林求size域 2.分析: 这个就是一个动态树问题,对于每一个位置i有i+ki这个父亲, 于是这就是一个森林了,然后对于每一个修改直接lct维护就好,询问就是i到最外面 ...