首页 > 技术文章 > Spring MVC中的DispatcherSevlet和ContextLoaderListener

mswangblog 2017-03-15 21:07 原文

1、xml中的配置:

  把DispatcherSevlet和ContextLoaderListener一起配置到web.xml之中,当tomcat启动的时候,会创建这两个类型。然而这两个类型完成的工作却不是相同的。

对于DispatcherSevlet是加载包含web组件的bean(控制器、视图解析器和处理器映射),而ContextLoaderListener是加载应用中的其他bean(通常是应用后端的中间层和数据层组件)。如果让其加载的话,需要给它们传递应用上下文,让它们取加载。

所以根据它们不同的性质,取传递不同的应用上下文。

 

对于DispatcherSevlet传递的xml中的上下文通常叫做SpringMVC.xml

a、自动扫描的包是@Controller注解的

b、视图解析器

等等

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="sse.eprint"
        use-default-filters="false">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
        <context:include-filter type="annotation"
            expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan>

    <!-- 配置视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 配置静态资源 -->
    <mvc:default-servlet-handler />
    <!-- 启动注解扫描功能 -->
    <mvc:annotation-driven />
</beans>

 对于ContextLoaderListener传递的xml中的上下文通常叫做applicationContext.xml

在xml中是:数据bean、中间层等等

<?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:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="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-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 配置自动扫描的包 -->
        <context:component-scan base-package="ustc.sse.eprint">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation"
            expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan>


<!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"  destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql:///Eprint" />
        <property name="username" value="root" />
        <property name="password" value="admin" />
        <!-- 连接池启动时的初始值 -->
        <property name="initialSize" value="3" />
        <!-- 连接池的最大值 -->
        <property name="maxActive" value="500" />
        <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
        <property name="maxIdle" value="2" />
        <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
        <property name="minIdle" value="1" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingResources">
            <list>
                <value>ustc/sse/eprint/domain/AdminLog.hbm.xml</value></list>
        </property>
        <property name="hibernateProperties">
            <props>
                <!-- 数据库的方言 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
    
    <!-- 配置事务管理器统一管理SessionFactory的事务 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 启用事务注解 --> <!-- 还需要在相应的类上进行@Transactional注解或对应的方法上 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <bean id="basicDao" class="sse.eprint.basicdao.BasicDao">
    <property name="sessionFactory" ref="sessionFactory"></property>    
    </bean>
    
    </beans>
    

 

 2、通过Java配置文件

 

推荐阅读