Spring MVC项目中通常会有二个配置文件,spring-servlet.xml和applicationContext.xml二个配置文件,通常会出现以下几个配置:
1. <context:annotation-config />
它的作用是隐式地向 Spring 容器注册
- AutowiredAnnotationBeanPostProcessor、
- CommonAnnotationBeanPostProcessor、
- PersistenceAnnotationBeanPostProcessor、
- RequiredAnnotationBeanPostProcessor 这4个BeanPostProcessor。
其作用是如果你想在程序中使用注解,就必须先注册该注解对应的类,如下图所示:
依赖的类 | 注解 |
CommonAnnotationBeanPostProcessor | @Resource 、@PostConstruct、@PreDestroy |
PersistenceAnnotationBeanPostProcessor | @PersistenceContext |
AutowiredAnnotationBeanPostProcessor | @Autowired |
RequiredAnnotationBeanPostProcessor | @Required |
当然也可以自己进行注册:
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
2. <context:component-scan base-package="com.*" >
<context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。
在这里有一个比较有意思的问题,就是扫描是否需要在二个配置文件都配置一遍,我做了这么几种测试:
(1)只在applicationContext.xml中配置如下
<context:component-scan base-package="com.login" />
启动正常,但是任何请求都不会被拦截,简而言之就是@Controller失效
(2)只在spring-servlet.xml中配置上述配置
启动正常,请求也正常,但是事物失效,也就是不能进行回滚
(3)在applicationContext.xml和spring-servlet.xml中都配置上述信息
启动正常,请求正常,也是事物失效,不能进行回滚
(4)在applicationContext.xml中配置如下
<context:component-scan base-package="com.login" />
在spring-servlet.xml中配置如下
<context:component-scan base-package="com.sohu.login.web" />
此时启动正常,请求正常,事物也正常了。
结论:在spring-servlet.xml中只需要扫描所有带@Controller注解的类,在applicationContext中可以扫描所有其他带有注解的类(也可以过滤掉带@Controller注解的类)。
3. <mvc:annotation-driven />
它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter
相关推荐
注解和注释以及Spring和SpringMVC常用的注解 1.两者区别 注解 :参与代码编译,以@开头的。它是给应用程序看的,单独使用注解毫无意义,一定要跟工具一起使用,这个所谓的工具实际就是能读懂注解的应用程序 注释 :...
SpringMvc Spring Mybatis Maven 注解方式
spring+springMvc+MyBatis+注解
Spring+Springmvc+Mybatis注解开发配置,简单实现增删改差,访问路径为 http://localhost:8080/项目名/student/loadList.do,就可以实现效果,
基于SpringMVC以及Spring,Hibernate的非注解入门小demo
Spring和SpringMVC面试问题总结 1.Spring中AOP的应用场景、Aop原理、好处? 2.Spring中IOC的作用与原理?对象创建的过程。 3.介绍spring框架 4.Spring常见创建对象的注解? 5.Spring中用到的设计模式 6.Spring的优点...
Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+...
完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统...
spring+mybatis+springmvc maven框架,单模块,导入就可以使用,里面有一个user的增删查改,大家拿来修改就可以使用
spring和springMVC整合源码的helloworld版。
传智播客Java进阶Spring和springMVC详细示例精通教程资料 传智播客Java进阶Spring和springMVC详细示例精通教程资料 传智播客Java进阶Spring和springMVC详细示例精通教程资料
spring+springMvc+mybatis完美整合项目,内含数据库相关文件,导入即可运行,如有疑问请留意。
springMVC+Mybatis3 0 2+spring3 0 5(包含事务详解 代码诠释 含数据库文件) 注解实现自动注入 展示了增 删 改 查 注解 sql文件 经过严密测试绝对可以运行起来
Spring+SpringMvc+MybatisPlus实现多数据源切换、利用自定义Aop注解,只需要在需要切换数据库的方法上加上注解即可实现、极大避免了代码冗余。
Spring和SpringMVC父子容器关系视频,视频,视频,视频,重要的事情说了不止三遍。。。
spring+springMVC+BlazeDS注解方式的配置文件.
spring/springmvc/mybatis所需jar包
ssm-Spring和SpringMVC和MyBatis.zip
springMVC+spring+hibernate基于的注解整合
spring+springmvc+mybatis 适合练手,但是jdk版本不要太高,不然运行容易出现版本的问题