zoukankan      html  css  js  c++  java
  • spring 部分配置内容备忘

    部分资料来源:

    1. context:component-scan使用说明
    2. SpringMVC源码总结(一)HandlerMapping和HandlerAdapter入门
    3. springmvc中url-url-pattern /和/*的区别

    context:component-scan

    @Component是所有受Spring管理组件的通用形式,而 @Repository, @Service和 @Controller则是 @Component的细化,用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)。
    在xml配置了这个标签后,spring可以自动去扫描base-package下面或者子包下面的Java文件,如果扫描到有 @Component @Controller @Service等这些注解的类,则把这些类注册为bean。
    在说明这两个子标签前,先说一下context:component-scan有一个use-default-filters属性,该属性默认为true,这就意味着会扫描指定包下的全部的标有 @Component的类,并注册成bean.也就是 @Component的子注解 @Service, @Reposity等。

    <context:component-scan base-package="com.champion.ssqs.core.test_spring">
    </context:component-scan>
    

    扫描com.champion.ssqs.core.test_spring下全部标有 @Component的类,包括 @Controller, @Service, @Reposity等。

    如果use-default-filters属性设置为false 则需要在context:component-scan中配置要扫描的注解,来扫描对应的注解。

    <context:component-scan base-package="com.champion.ssqs.core.test_spring" use-default-filters="false">
    	<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
    </context:component-scan>
    

    扫描com.champion.ssqs.core.test_spring下全部标有 @Service的类,其余不扫描。

    <context:component-scan base-package="com.champion.ssqs.core.test_spring" use-default-filters="false">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    

    扫描com.champion.ssqs.core.test_spring下全部没有标有 @Controller的类,其余不扫描。

    HandlerMapping,HandlerAdapter

    <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。 <mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
    实际上在3.1之后,<mvc:annotation-driven />注册的类发生了变化
    Spring Framework 3.1 introduces a new set of support classes for processing requests with annotated controllers:

    RequestMappingHandlerMapping
    RequestMappingHandlerAdapter
    ExceptionHandlerExceptionResolver

    These classes are a replacement for the existing:

    DefaultAnnotationHandlerMapping
    AnnotationMethodHandlerAdapter
    AnnotationMethodHandlerExceptionResolver

    The above registers a RequestMappingHandlerMapping , a RequestMappingHandlerAdapter , and an ExceptionHandlerExceptionResolver (among others) in support of processing requests with annotated controller methods using annotations such as
    @RequestMapping , @ExceptionHandler , and others.

    It also enables the following:

    Spring 3 style type conversion through a ConversionService instance in addition to the JavaBeans PropertyEditors used for Data Binding.

    Support for formatting Number fields using the @NumberFormat annotation through the ConversionService .

    Support for formatting Date, Calendar, Long, and Joda Time fields using the @DateTimeFormat annotation.

    Support for validating @Controller inputs with @Valid , if a JSR-303 Provider is present on the classpath.

    HttpMessageConverter support for @RequestBody method parameters and @ResponseBody method return values from @RequestMapping or @ExceptionHandler methods.

    This is the complete list of HttpMessageConverters set up by mvc:annotation-driven:

    ByteArrayHttpMessageConverter converts byte arrays.

    StringHttpMessageConverter converts strings.

    ResourceHttpMessageConverter converts to/from org.springframework.core.io.Resource for all media types.

    SourceHttpMessageConverter converts to/from a javax.xml.transform.Source .

    FormHttpMessageConverter converts form data to/from a MultiValueMap<String, String> .

    Jaxb2RootElementHttpMessageConverter converts Java objects to/from XML — added if JAXB2 is present on the classpath.

    MappingJackson2HttpMessageConverter (or MappingJacksonHttpMessageConverter ) converts to/from JSON — added if Jackson 2 (or Jackson) is present on the classpath.

    AtomFeedHttpMessageConverter converts Atom feeds — added if Rome is present on the classpath.

    RssChannelHttpMessageConverter converts RSS feeds — added if Rome is present on the classpath.

    这是摘取的官方文档

    HandlerMapping接口的实现(常见类型) :

    BeanNameUrlHandlerMapping :通过对比url和bean的name找到对应的对象
    SimpleUrlHandlerMapping :也是直接配置url和对应bean,比BeanNameUrlHandlerMapping功能更多
    DefaultAnnotationHandlerMapping : 主要是针对注解配置 @RequestMapping的,已过时
    RequestMappingHandlerMapping(3.2之前是使用DefaultAnnotationHandlerMapping 这个去处理,但是后来弃用了) :取代了上面一个,现在默认的请求匹配方式,通过 @RequestMapping 注解来决定调用方法和具体的类,也是最常用的一种

    如果没有明确声明任何处理器映射,spring会默认使用BeanNameUrlHandlerMapping,但如果明确声明了其它的处理器映射,则需要将BeanNameUrlHandlerMapping明确声明出来,而且在每个包含被映射的bean的配置文件中都要加入BeanNameUrlHandlerMapping

    HandlerAdapter 接口实现:
    HttpRequestHandlerAdapter : 要求handler实现HttpRequestHandler接口,该接口的方法为 void handleRequest(HttpServletRequest request, HttpServletResponse response)也就是 handler必须有一个handleRequest方法。
    SimpleControllerHandlerAdapter:要求handler实现Controller接口,该接口的方法为ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response),也就是本工程采用的
    AnnotationMethodHandlerAdapter :和上面的DefaultAnnotationHandlerMapping配对使用的,也已过时
    RequestMappingHandlerAdapter : 和上面的RequestMappingHandlerMapping配对使用,针对 @RequestMapping

    举例如下:

    spring.xml配置
    <bean name="/index" class="com.lg.mvc.HomeAction"></bean> 
    
    controller内容
    public class HomeAction implements Controller{  
    
        @Override  
        public ModelAndView handleRequest(HttpServletRequest request,  
                HttpServletResponse response) throws Exception {  
            return new ModelAndView("hello");  
        }  
    }   
    

    先简单的说下这个工程的流程,访问http://localhost:8080/index首先由DispatcherServlet进行转发,通过BeanNameUrlHandlerMapping(含有 /index->HomeAction的配置),找到了HomeAction,然后再拿HomeAction和每个adapter进行适配,由于HomeAction实现了Controller接口,所以最终会有SimpleControllerHandlerAdapter来完成对HomeAction的handleRequest方法的调度。然后就顺利的执行了我们想要的方法。

    url-pattern /和/*的区别

    < url-pattern > / </ url-pattern > 不会匹配到*.jsp,即:*.jsp不会进入spring的 DispatcherServlet类 。
    < url-pattern > /* </ url-pattern > 会匹配*.jsp,会出现返回jsp视图时再次进入spring的DispatcherServlet 类,导致找不到对应的controller所以报404错。

    总之,关于web.xml的url映射的小知识:
    / 会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url
    /* 会匹配所有url:路径型的和后缀型的url(包括/login,*.jsp,*.js和*.html等)

    IOC(DI)

    IOC理论的意义就是将复杂的对象间的关系分解成相互合作的对象,通过封装,对象类实现对外部是透明的,从而降低了问题的复杂度,而且还可以灵活的被重用和扩展(将对象 间的耦合关系断开,控制权全部交由第三方,所以IOC容器是整个系统的关键核心。

    依赖注入:
    IOC的另外名字叫做依赖注入(Dependency Injection),所谓的依赖注入,就是由IOC容器在运行期间,动态的将某种依赖关系注入到对象中,所以,依赖注入(DI)和控制反转(IOC)是从不同的角度的描述的同一件事情,就是指通过引入IOC容器,利用依赖关系注入的方式,实现对象见的解耦。

    IOC的好处:
    IOC在编程过程中不会对业务对象构成很强的侵入性,使用IOC之后,对象具有更好的可实行性,可重用性和可扩展性:
    1、 降低组件之间的耦合性
    2、 提高开发效率和产品质量
    3、 统一标准,提高模块的可复用性
    4、 模块具有热插拔特性

    IOC的通俗理解:
    1、 IOC控制反转:说的是创建对象实例的控制权从代码控制剥离到IOC容器控制,实际就是你在XML文件控制,侧重于原理
    2、 DI依赖注入:说的是创建对象实例时,为这个对象注入属性值或其他对象实例,侧重于实现

  • 相关阅读:
    哈希表(开放定址法处理冲突)(1013)
    哈希表(链地址法处理冲突)(1012)
    求最小生成树(Prim算法)(1075)
    POJ 1061 青蛙的约会(扩展欧几里得)
    小偷的背包(0963)
    20169208 2016-2017-2《网络攻防实践》课程总结
    20169208 2016-2017-2 《网络攻防实践》第十四周学习总结
    20169208 2016-2017-2 《网络攻防实践》第十一周学习总结
    20169208 2016-2017-2 《网络攻防实践》第10周学习总结
    20169208 2016-2017-2 《网络攻防实践》nmap扫描实践
  • 原文地址:https://www.cnblogs.com/cuiyf/p/6846808.html
Copyright © 2011-2022 走看看