zoukankan      html  css  js  c++  java
  • Spring一些疑惑点学习

    1、Spring  contextConfigLocation配置

    如果是 <context-param>标签内的,如果没有提供值,默认会去/WEB-INF/config/applicationContext.xml

     <!-- avalible during applicatio -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

    如果是 <init-param>标签内的,如果没有提供值,默认会去找/WEB-INF/*-servlet.xml

     <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 配置SpringMVC 需要配置的文件
            spring-dao.xml,spring-service.xml,spring-web.xml
            Mybites -> spring -> springMvc  -->
            <!--avalible in servlet init()-->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:/spring/spring-*.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>

    2、spring配置注解context:annotation-config和context:component-scan、以及<mvc:annotation-driven>

    (1)context:annotation-config的作用

    < context:annotation-config>的作用是用于激活那些已经在spring容器里注册过的bean上面的注解,也就是显示的向spring注册下面这四个Processor

    AutowiredAnnotationBeanPostProcessor
    CommonAnnotationBeanPostProcessor
    PersistenceAnnotationBeanPostProcessor
    RequiredAnnotationBeanPostProcessor

    比如我们要使用@Autowired注解,那么就必须事先在spring容器里声明一个AutowiredAnnotationBeanPostProcessor的Bean,传统的声明方式如下:

    <bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>

    同理@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor

    如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean

    如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean

    但是这里需要注意一下单纯使用< context:annotation-config/>并没有激活@Component、@Controller、@Service等这些注解

    (2)context:component-scan的作用

    <context:component-scan base-package=”XX.XX”/> 

    该配置项其实也包含了context:annotation-config自动注入的四个processor 的功能,因此当使用 < context:component-scan/> 后,就可以将 < context:annotation-config/> 移除了。 

    除此之外,还具有自动将带有@component,@service,@Repository等注解的对象注册到spring容器中的功能。

    (3)如果同时使用context:annotation-config和context:component-scan?

    因为< context:annotation-config />和 < context:component-scan>同时存在的时候,前者会被忽略。如@autowire,@resource等注入注解只会被注入一次!

    (4)<mvc:annotation-driven>的作用

    < mvc:annotation-driven /> 是一种简写形式。< mvc:annotation-driven/>主要为了Spring MVC用的,提供Controller请求转发,json自动转换等功能。会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean。是spring MVC为@Controllers分发请求所必须的。 
    并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。

  • 相关阅读:
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
    C语言II博客作业04
    C语言II博客作业03
    C语言II博客作业01
    学期总结
    C语言I博客作业08
    C语言I博客作业07
  • 原文地址:https://www.cnblogs.com/wylwyl/p/13304550.html
Copyright © 2011-2022 走看看