Spring MVC配置文件的三个常用配置详解
来源:互联网 发布:数据库表关联 编辑:程序博客网 时间:2024/04/20 20:52
Spring MVC项目中通常会有二个配置文件,sprng-servlet.xml和applicationContext.xml二个配置文件,通常会出现以下几个配置
1. <context:annotation-config />
它的作用是隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor 这4个BeanPostProcessor。其作用是如果你想在程序中使用注解,就必须先注册该注解对应的类,如下图所示:
依赖的类注解CommonAnnotationBeanPostProcessor@Resource 、@PostConstruct、@PreDestroyPersistenceAnnotationBeanPostProcessor的Bean@PersistenceContextAutowiredAnnotationBeanPostProcessor Bean@AutowiredRequiredAnnotationBeanPostProcessor@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" />
扫描到有@RestController、@Controller、@Compoment、@Repository等注解的类,则把这些类注册为Bean。
启动正常,但是任何请求都不会被拦截,简而言之就是@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注解的类)。
详解 http://blog.csdn.net/gladmustang/article/details/39999721
3. <mvc:annotation-driven />
它会自动注册RequestMappingHandlerMapping 与RequestMappingHandlerAdapter ( 3.1 以后DefaultAnnotationHandlerMapping 和AnnotationMethodHandlerAdapter已经可以不用了)
详情
http://www.cnblogs.com/yangzhilong/p/3725849.html
http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null
官方文档: http://docs.spring.io/spring/docs/4.2.2.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#beans-autowired-annotation
区别: http://my.oschina.net/HeliosFly/blog/205343
###################################################################################################
spring中classpath和classpath*的配置区别
我们以spring中加载properties资源文件为例讲解,目录结构大致如下:
同时 在该项目的lib中添加一个测试的micmiu-test.jar包,jar包中的文件结构如下:
从准备的测试环境中我们可以看到在不同目录下的四个同名的application.properties资源文件。
[二]、测试代码:TestClassPath.java
package com.micmiu.demoweb; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestClassPath { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml"); System.out.println(ctx.getClassLoader().getResource("").getPath()); }}
spring配置文件:applicationContext.xml 中两种不同的properties文件加载配置:
第一种配置:classpath:
<context:property-placeholder ignore-unresolvable="true" location="classpath:/application.properties" />输出:
第二种配置: classpath*:
由此日志信息可知:
- 同名资源存在时,classpath: 只从第一个符合条件的classpath中加载资源,而classpath*: 会从所有的classpath中加载符合条件的资源
- classpath*:需要遍历所有的classpath,效率肯定比不上classpath,因此在项目设计的初期就尽量规划好资源文件所在的路径,避免使用classpath*来加载
- Spring MVC配置文件的三个常用配置详解
- Spring MVC配置文件的三个常用配置详解
- Spring MVC配置文件的三个常用配置详解
- Spring MVC配置文件的三个常用配置详解
- Spring MVC配置文件的三个常用配置详解
- Spring MVC配置文件的三个常用配置详解
- Spring MVC配置文件的三个常用配置详解(annotation-config,annotation-config,annotation-driven)
- spring mvc 三个常用配置
- Spring的MVC配置详解
- spring-mvc.xml 配置文件详解
- Spring MVC的配置文件
- spring mvc常用配置
- 详解Spring MVC 4常用的注解
- 详解Spring MVC 常用的那些注解
- Spring MVC配置详解
- Spring MVC配置详解
- spring MVC配置详解
- spring MVC配置详解
- ios学习--Runtime 基础知识
- bat根据当前时间生成文件名称
- Spark系列(七)Master中的资源调度
- Property文件读取
- SimpleDateFormate日期字符串转换(墨雪亲测)
- Spring MVC配置文件的三个常用配置详解
- Spark系列(八)Worker工作原理
- Spark系列(九)DAGScheduler工作原理
- 在Qt中使一个控件的内容实时随着另一个控件中的内容变化
- 安卓开发规范参考
- Spark系列(十)TaskSchedule工作原理
- 数据结构(五)位图算法
- ApacheBeam:大数据处理的一大神器!
- INT强制转化BYTE数组的规则