• Spring MVC启动过程


    org.springframework.web.context.ContextLoaderListener

    ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法.

    如果在web.xml中不写任何参数配置信息,默认的路径是”/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:

    1. <context-param> 
    2. <param-name>contextConfigLocation</param-name> 
    3. <param-value> 
    4. /WEB-INF/classes/applicationContext-*.xml 
    5. </param-value> 
    6. </context-param>

    在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔。上面的applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。

    由此可见applicationContext.xml的文件位置就可以有两种默认实现:

    第一种:直接将之放到/WEB-INF下,之在web.xml中声明一个listener、

    第二种:将之放到classpath下,但是此时要在web.xml中加入<context-param>,用它来指明你的applicationContext.xml的位置以供web容器来加载。按照 Struts2 整合spring的官方给出的档案,写成:

    1. <!-- Context Configuration locations for Spring XML files --> 
    2. <context-param> 
    3. <param-name>contextConfigLocation</param-name> 
    4. <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> 
    5. </context-param>

    ContextLoaderListener初始化的上下文加载的Bean是对于整个应用程序共享的,不管是使用什么表现层技术,一般如DAO层、Service层Bean;

    DispatcherServlet初始化的上下文加载的Bean是只对Spring Web MVC有效的Bean,如Controller、HandlerMapping、HandlerAdapter等等,该初始化上下文应该只加载Web相关组件。

    下面给出applicationContext.xml的例子,可见,他是在初始化系统的东西,如mysql,mybatis,数据库等,但是排除了spirngmvc的controller。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
        default-lazy-init="true">
    
        <description>Spring公共配置</description>
    
        <!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->
        <context:component-scan base-package="com.XXXXX.server">
            <context:exclude-filter type="annotation"
                expression="org.springframework.stereotype.Controller" />  //排除springMVC controller
        </context:component-scan>
    
        <!-- MyBatis配置 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="tddlDataSource" />
            <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
            <property name="typeAliasesPackage"
                value="com.XXXXX.server.bean.entity" />
            <!-- 显式指定Mapper文件位置 -->
            <property name="mapperLocations" value="classpath:/mybatis/*Mapper.xml" />
        </bean>
    
        <!-- 扫描basePackage下所有以@MyBatisRepository标识的 接口 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.XXXXX.server.dal.dao" />
            <property name="annotationClass"
                value="com.XXXXX.server.dal.dao.MyBatisRepository" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        </bean>
    
        <!-- 事务管理器配置, 单数据源事务 -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
            p:dataSource-ref="tddlDataSource" />
    
        <!-- 使用annotation定义事务 -->
        <tx:annotation-driven transaction-manager="transactionManager"
            proxy-target-class="true" />
       
    
        <bean id="stringHttpMessageConverter"
            class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-arg value="UTF-8" />
        </bean>
    
    </beans

    下面给出spring-mvc.xml例子,我们这里只扫描controller。可见,他只初始化和spingmvc有关的数据

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
        <!-- 自动扫描且只扫描@Controller -->
        <context:component-scan base-package="com.XXXXX.server.web" use-default-filters="false">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
        
        <mvc:annotation-driven/>
            
        <!-- 将无法mapping到Controller的path交给default servlet handler处理 -->        
        <mvc:default-servlet-handler/>
        
        <!-- 定义JSP文件的位置 --> 
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
        
        <!-- 定义无Controller的path<->view直接映射 -->
        <mvc:view-controller path="/" view-name="redirect:/index"/>
    </beans>
  • 相关阅读:
    TypeScript学习: 一、TypeScript的Hello World
    着重查看
    表内灌数
    jvm
    GitHub如何使用命令过滤筛选你想要的项目源代码
    [CVPR2021]Beyond Self-attention External Attention using Two Linear Layers for Visual Tasks
    [CIKM2019] AutoInt: Automatic Feature Interaction Learning via Self-Attentive Neural Networks
    [IJCAI2017] Attentional Factorization Machines: Learning the Weight of Feature Interactions via Attention Networks
    [SIGIR2020] Sequential Recommendation with Self-Attentive Multi-Adversarial Network
    ES6 Promise 的链式调用
  • 原文地址:https://www.cnblogs.com/diegodu/p/6005229.html
Copyright © 2020-2023  润新知