为啥Spring和Spring MVC包扫描要分开?
背景:
最近在搭建新工程的时候发现有些Spring的配置不是很了解,比如Spring 配置里面明明配置了component-scan,为啥Spring MVC配置文件还需要配置一下,这样岂不是多此一举?由于以前基本是在现有的工程上直接开发或者别的工程的配置文件直接拷贝过来,所以也没太关注这个问题。出于好奇,谷歌了一下发现原来这个里面大有学问呢,详情请见下文。正常代码如下:
- <!-- spring 配置文件-->
- <context:component-scan base-package="com.xxx.xxx.account.front">
- <context:exclude-filter type="annotation"
- expression="org.springframework.stereotype.Controller" />
- </context:component-scan>
- <!-- spring mvc -->
- <context:component-scan base-package="com.xxx.xxx.account.front.web" use-default-filters="false">
- <context:include-filter type="annotation"
- expression="org.springframework.stereotype.Controller" />
- </context:component-scan>
测试bean
- @Service
- public class TestService implements InitializingBean {
- @Autowired
- private PersonalAddressAjaxController personalAddressAjaxController;
- @Override
- public void afterPropertiesSet() throws Exception {
- System.out.println("--------------------------------");
- }
- }
原理:
原来Spring 是父容器, Spring MVC是子容器, 子容器可以访问父容器的bean,父容器不能访问子容器的bean。
具体参照:
测试一: Spring加载全部bean,MVC加载Controller
- <!-- spring 配置文件-->
- <context:component-scan base-package="com.xxx.xxx.account.front">
- </context:component-scan>
- <!-- spring mvc -->
- <context:component-scan base-package="com.xxx.xxx.account.front.web" use-default-filters="false">
- <context:include-filter type="annotation"
- expression="org.springframework.stereotype.Controller" />
- </context:component-scan>
测试结果:TestService通过,界面显示正常。
原因:父容器加载了全部bean,所以Service 能访问到Controller。MVC容器默认查找当前容器,能查到有转发的Controller规则所以界面正常跳转。
测试二:Spring加载全部Bean,MVC容器啥也不加载
- <!-- spring 配置文件-->
- <context:component-scan base-package="com.xxx.xxx.account.front">
- </context:component-scan>
- <!-- spring mvc -->
- 无
测试结果:TestService通过,界面显示404。
原因:父容器加载了全部bean,所以Service 能访问到Controller。MVC容器默认查找当前容器的Controller,找不到所以界面出现404。
测试三:Spring加载所有除了Controller的bean,MVC只加载Controller
- <!-- spring 配置文件-->
- <context:component-scan base-package="com.xxx.xxx.account.front">
- <context:exclude-filter type="annotation"
- expression="org.springframework.stereotype.Controller" />
- </context:component-scan>
- <!-- spring mvc -->
- <context:component-scan base-package="com.xxx.xxx.account.front.web" use-default-filters="false">
- <context:include-filter type="annotation"
- expression="org.springframework.stereotype.Controller" />
- </context:component-scan>
测试结果:TestService初始化失败,如果注释掉该bean,界面正常。
原因:父容器不能访问子容器的bean。
测试四:Spring不加载bean,MVC加载所有的bean
- <!-- spring 配置文件-->
- 无
- <!-- spring mvc -->
- <context:component-scan base-package="com.xxx.xxx.account.front.web" use-default-filters="true">
- </context:component-scan>
测试结果:TestService通过,界面正常。
原因:因为所有的bean都在子容器中,也能查到当前容器中的Controller,所以没啥问题。
疑问一: 单例的bean在父子容器中存在一个实例还是两个实例?
答:初始化两次,Spring 容器先初始化bean,MVC容器再初始化bean,所以应该是两个bean。
疑问二:为啥不把所有bean 都在子容器中扫描?
答: 网上很多文章说子容器不支持AOP,其实这是不对的。因为正常会有AOP的相关配置都在Spring容器中配置,如果都迁移到MVC配置文件,则所有bean都在子容器中,相当于只有一个容器了,所以也就实现了AOP。缺点是不利于扩展。
为啥Spring和Spring MVC包扫描要分开?的更多相关文章
- 为啥Spring和Spring MVC包扫描要分开
开始学习springmvc各种小白问题 根据例子配置了spring扫描包,但是一直提示404错误,经过大量搜索,发现,扫描包的配置应该写在springmvc的配置文件中,而不是springmvc 配置 ...
- Spring和Spring MVC包扫描
在Spring整体框架的核心概念中,容器是核心思想,就是用来管理Bean的整个生命周期的,而在一个项目中,容器不一定只有一个,Spring中可以包括多个容器,而且容器有上下层关系,目前最常见的一种场景 ...
- spring和springmvc包扫描问题
写这篇博客之前,橘子松必须感慨下!!找了我一下午加一晚上(md),问了几个朋友也没找到.凉了啊 在搭建ssm之前,我把controller service mapper包扫描用基本包扫描 都写在a ...
- spring 排除指定的类或者包扫描
<!-- 排除Controller注解的扫描 --> <context:component-scan base-package="exampleBean"> ...
- Spring IoC 源码分析 (基于注解) 之 包扫描
在上篇文章Spring IoC 源码分析 (基于注解) 一我们分析到,我们通过AnnotationConfigApplicationContext类传入一个包路径启动Spring之后,会首先初始化包扫 ...
- 整合Spring时Service层为什么不做全局包扫描详解
合Spring时Service层为什么不做全局包扫描详解 一.Spring和SpringMVC的父子容器关系 1.讲问题之前要先明白一个关系 一般来说,我们在整合Spring和SpringMVC这两个 ...
- 不会吧,有人用了两年Spring, 居然不知道包扫描是怎么实现的
全栈的自我修养: 0004 Java 包扫描实现和应用(File篇) I may not be able to change the past, but I can learn from it. 我也 ...
- Spring源码分析-从@ComponentScan注解配置包扫描路径到IoC容器中的BeanDefinition,经历了什么(一)?
阅前提醒 全文较长,建议沉下心来慢慢阅读,最好是打开Idea,点开Spring源码,跟着下文一步一步阅读,更加便于理解.由于笔者水平优先,编写时间仓促,文中难免会出现一些错误或者不准确的地方,恳请各位 ...
- spring盒springMVC整合父子容器问题:整合Spring时Service层为什么不做全局包扫描详解
整合Spring时Service层为什么不做全局包扫描详解 一.Spring和SpringMVC的父子容器关系 1.讲问题之前要先明白一个关系 一般来说,我们在整合Spring和SpringMVC这两 ...
随机推荐
- HBase 协处理器统计行数
环境:cdh5.1.0 启用协处理器方法1. 启用协处理器 Aggregation(Enable Coprocessor Aggregation) 我们有两个方法:1.启动全局aggregation, ...
- poj1769 Minimizing maximizer
传送门 题目大意 给你m个机器,n个数,每个机器可以给n个数的某一段排序,求最少使用几个机器,保证可以把这个n个数排好序 分析 我们可以想到dpij表示考虑前i个机器让最大的数到达点j至少需要使用多少 ...
- 《Maven实战》笔记-4-生命周期和插件
除了坐标.依赖以及仓库外,Maven另外两个核心概念是生命周期和插件. 一.生命周期 Maven的生命周期是抽象的,其本身不做任务实际的工作,实际的任务(如编译源代码)都交由插件来完成. 三套生命周期 ...
- [学习笔记]fork写实复制
#include<stdio.h> #include<stdlib.h> #include<string.h> #include <unistd.h> ...
- Xshell连接不上Linux的解决方法
xshell连接linux主机时,会出现错误:Could not connect to '127.0.0.1' (port 22): Connection failed. 但是这时能ping通. 通 ...
- ASP.NET MVC 之各种jQuery提交模式实例
1.$.ajax提交 var _data = { "dictItemID": dictItemID, "itemType": itemType, "i ...
- Linq 和 Lambda 查询中按照多个值进行分组GroupBy
创建要查询的对象: class Employee { public int ID { get;set; } public string FName { get; set; } public int A ...
- Python3中实现简单的购物车程序
product_list = [ ('iphone',5800), ('imac',15800), ('watch',9800), ('cloth',550), ('coffe latee',35), ...
- [poj 2106] Boolean Expressions 递归
Description The objective of the program you are going to produce is to evaluate boolean expressions ...
- kuangbin专题K(next数组)
题目链接: https://vjudge.net/contest/70325#problem/K 题意: 给出一个字符串 str, 求 str 的所有前缀总共出现的次数. 思路: 先求一次 next ...