`
忆梦竹
  • 浏览: 64783 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

Spring 一些标签的作用,ContextLoaderListener和DispatcherServlet两者的配置文件的区别

阅读更多
本文将这样展开:
1. 简单讲解Spring中的<context:component-scan /> <context:annotation-config /> 
<mvc:annotation-driven /> 这三者的作用
2. 讲解下我们经常看到的在web.xml文件中定义的ContextLoaderListener和DispatcherServlet的区别

<context:annotation-config />
这个标签告诉Spring到bean类中寻找一些annotation定义的类,
这些annotation基本如下:
@Autowired  @PostConstruct  @PreDestroy  @Resource 等。
需要注意的是。这个标签并没有激活@Transactional 和 @TransactionAttribute

<context:component-scan base-package=""/>
这个标签用于告诉Spring搜索我们指定的包下面以及一些需要被自动注入的bean。
默认支持的annotation:@Component @Repository  @Service @Controller
需要注意的是:这个标签页实现了和annotation-config同样的效果。

<mvc:annotation-driven />
刚开始的时候我也很困惑为什么需要配置这个标签,但是当我们在配置SpringMVC的控制器的时候,我们发现有RequestMapping的存在,这个标签的作用之一就是告诉Spring去检测RequestMapping。其他的作用如下:
- 激活@ExceptionHandler这个annotation
- 配置了这个标签还可以将RequestMappingHandlerAdapter注册到Spring中
- 是SpringMVC提供默认的类型转化,因为我们没有在<mvc:annotation-driven/> 的属性中配置ConversionService
- 当然,这个标签还起到了一些其他的作用,我不怎么会翻译,怕变味。下面是英文原文:

It also enables the following:
1. Spring 3 style type conversion through a ConversionService instance in addition to the JavaBean PropertyEditors used for Data Binding.
2. Support for formatting Number fields using the @NumberFormat annotation through the ConversionService
3. Support for formatting Data,Calendar,Long and Joda Time fields using the @DateTimeFormat annotation, if Joda Time 1.3 or higher is present on the classpath.
4. Support for validating @Controller inputs with @Valid if a JSR-303 Provider is present on the classpath.
5. HttpMessageConverter support for @RequestBody method parameters and @ResponseBody method return values form @RequestMapping or @ExceptionHandler methods.


2.  下面是讲解web.xml配置是ContextLoaderListener和DispatcherServlet的区别:
- ContextLoaderListener是在我们的web容器启动的时候启动的,默认会加载/WEB-INF/下面的applicationContext.xml文件。并创建一个WebApplicationContext容器。
- DispatcherServlet是在我们第一次访问我们的应用的时候创建的。这时候它默认会将配置在/WEB-INF下面的<servlet-name>-servlet.xml配置文件,然后也创建一个WebApplicationContext。这个WebApplicationContext将之前ContextLoaderListener创建的容器作为父容器,因此在父容器中配置的所有Bean都能够被注入到子容器中。


1
3
分享到:
评论

相关推荐

    Spring的监听器ContextLoaderListener的作用

    Spring的监听器ContextLoaderListener的作用

    DispatcherServlet 和 ContextLoaderListener 区别

    NULL 博文链接:https://angie.iteye.com/blog/2334955

    Web项目中使用Spring, 使用 Spring 的器监听器 ContextLoaderListener.docx

    指定 Spring 配置文件的位置 o4. 获取Spring容器对象 在 Web 项目中使用 Spring 框架,首先要解决在 web 层(这里指 Servlet)中获取到 Spring容器的问题。只要在 web 层获取到了 Spring 容器,便可从容器中获取到 ...

    java解决org.springframework.web.context.ContextLoaderListener

    java解决org.springframework.web.context.ContextLoaderListener

    Spring MVC 入门实例

    上面, 我们在 web.xml 文件中告诉 ContextLoaderListener, 我们还有另外两个配置文件 /WEB-INF/database.xml 和 /WEB-INF/applicationContext.xml. applicationContext.xml: 1 2 &lt;!DOCTYPE beans PUBLIC "-//...

    整合struts2和spring源代码(可以直接在tomcat中运行)

    当创建一个对象的时候,它会用Struts2配置文件中的class属性去和Spring配置文件中的id属性进行关联,如果能找到,则由Spring创建,否则由Struts 2框架自身创建,然后由Spring来装配。Spring插件具体有如下几个作用:...

    springweb3.0MVC注解(附实例)

    web.xml 中定义了一个名为 annomvc 的 Spring MVC 模块,按照 Spring MVC 的契约,需要在 WEB-INF/annomvc-servlet.xml 配置文件中定义 Spring MVC 模块的具体配置。annomvc-servlet.xml 的配置内容如下所示: ...

    web.xml中ContextLoaderListener的运行过程解析

    web.xml中ContextLoaderListener的运行

    spring和hibernate整合

    org.springframework.web.context.ContextLoaderListener &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt; &lt;/listener&gt;

    简单spring MVC 配置

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener &lt;servlet-name&gt;test &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet &lt;load-on-startup&gt;1 &lt;servlet-name&gt;...

    Spring源码学习七:web应用自动装配Spring配置文件1

    在web应用启动读取web.xml时,发现配置了ContextLoaderListener,而ContextLoaderListener实现了ServletCo

    spring-web-2.5.jar

    org.springframework.web.context.ContextLoaderListener.class org.springframework.web.context.ContextLoaderServlet.class org.springframework.web.context.ServletConfigAware.class org.springframework.web....

    Spring-5.1.5源码

    Spring framework 5.1.5源码 Main: spring-web/org.springframework.web.context.ContextLoaderListener

    springmybatis

    5. 在java 对象和 数据库之间有做mapping 的配置文件,也通常是xml 文件。 mybatis实战教程(mybatis in action)之一:开发环境搭建 mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis...

    spring_MVC源码

    弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...

    Spring MVC 框架应用实例

    org.springframework.web.context.ContextLoaderListener &lt;filter-name&gt;encodingFilter org.springframework.web.filter.CharacterEncodingFilter &lt;param-name&gt;encoding ...

    spring源代码解析

    从加载过程我们可以看到,首先从Servlet事件中得到ServletContext,然后可以读到配置好的在web.xml的中的各个属性值,然后ContextLoder实例化WebApplicationContext并完成其载入和初始化作为根上下文。当这个根上...

    struts2-spring-plugin-2.1.2.jar

    struts2与spring的整合。导入struts2-spring-... spring监听器对应的API类为:org.springframework.web.context.ContextLoaderListener。 struts2-spring-plugin包为我们将struts2的对象工厂设置为spring的IoC容器,

    iLink:拉丁云代码测试Spring MVC

    参考(2)Eclipse和IDEA配置Maven2.Spring版本:5.0.73.MVC:Spring MVC配置(1)jar包:spring-webmvc.jar(2)web.xml配置SpringMVC监听类:org.springframework.web.context.ContextLoaderListener(3)SpringMVC核心配置...

    ssh(structs,spring,hibernate)框架中的上传下载

    文件数据存储在Blob类型的FILE_CONTENT表字段上,在Spring中采用OracleLobHandler来处理Lob字段(包括Clob和Blob),由于在程序中不需要引用到oracle数据驱动程序的具体类且屏蔽了不同数据库处理Lob字段方法上的差别...

Global site tag (gtag.js) - Google Analytics