SpringMVC 常用配置

SpringMVC 配置中一般分为三个基本配置文件:Web.xml、ApplicationContext.xml、DispatcherServlet.xml(SpringMVC-Servlet.xml),不需要纠结后两者的命名规则,关键是知道他们的作用就行了,名字可以随便取,但为了代码有较强的可读性还是建议使用关键字来命名。

首先认识Web.xml,这也是整个项目的一个关键文件,所有的配置从这里开始,通用配置如下:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

         version="3.1">

    <!--配置监听器-->

    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/applicationContext.xml</param-value>

    </context-param>

    <!--配置Dispatcher控制器-->

    <servlet>

        <!—对应的文件名为dispatcher-servlet.xml-->

        <servlet-name>dispatcher</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>

        <servlet-name>dispatcher</servlet-name>

        <url-pattern>/</url-pattern>

    </servlet-mapping>

    <!--配置静态资源读取方式-->

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.js</url-pattern>

        <url-pattern>*.css</url-pattern>

        <url-pattern>/assets/*"</url-pattern>

        <url-pattern>/images/*</url-pattern>

    </servlet-mapping>

</web-app>

这个配置已经是比较简单直观,主要是两部分内容:配置DispatcherServlet和ContextLoaderListener,其中DispatcherServlet是加载包含Web组件的bean,如:控制器、视图解析器以及处理器映射;而ContextLoaderListener要加载应用中的其他bean,这些Bean通常是驱动应用后端的中间层和数据层组件或控制Spring的特性如AOP事务等,具体内容详见《Spring 实战4》-第五章。

再看DispatcherServlet对应的配置文件:dispatcher-servlet.xml和ContextLoaderListener对应的配置文件applicationContext.xml。

参考文件如下:

applicationContext.xml:

<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:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

     <!-- 配置组件扫描器 -->

     <!-- 在dispatcher-servlet.xml文件中也可以配置这个属性 -->  

     <context:component-scan base-package="com.test"/>

      

    <!-- 数据源 -->

    <bean id="dataSource"

        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />

        <property name="url" value="jdbc:mysql://localhost:3306/test" />

        <property name="username" value="root" />

        <property name="password" value="" />

    </bean>

    

    <!-- 配置session工厂 -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

        <property name="dataSource" ref="dataSource"/>

        <property name="configLocation" value=“classpath:???.xml"/>

    </bean>

    

    <!-- 事务管理器 -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource"/>

    </bean>

    

    <!-- 配置AOP通知 -->

    <tx:advice id="txAdvice" transaction-manager="transactionManager">

         <!-- 配置事务属性 -->

         <tx:attributes>

             <!-- 添加事务管理的方法 -->

             <tx:method name="save*" propagation="REQUIRED"/>

             <tx:method name="delete*" propagation="REQUIRED"/>

             <tx:method name="update*" propagation="REQUIRED"/>

             <tx:method name="select*" read-only="true"/>

         </tx:attributes>

     </tx:advice>

     

     <!-- 配置AOP,为添加事务管理的操作配置AOP -->

    <aop:config>

        <!-- 引入的Spring定义的事务通知,需要使用aop:advisor -->

        <!-- 下面难 -->

        <aop:advisor advice-ref="txAdvice"

            pointcut="execution(* com.test.service.*.*(..))"

        />

    </aop:config>

</beans>

dispatcher-servlet.xml:

<?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:context="http://www.springframework.org/schema/context" 

    xmlns:mvc="http://www.springframework.org/schema/mvc" 

    xmlns:task="http://www.springframework.org/schema/task"

    xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-4.2.xsd">    

        

        <!-- 配置组建扫描器 —>  

        <mvc:annotation-driven />

        <context:component-scan base-package="com.test.controller"/>

        <!—使用Web.xml默认的Servlet来响应静态文件-->

        <mvc:default-servlet-handler/>

        <!-- 视图解析器 —>

        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 

        <!-- 前缀 -->

        <property name="prefix”>

            <value>/WEB-INF/content/</value>

        </property>

        <!-- 后缀 -->

        <property name="suffix">

            <value>.jsp</value>

        </property>

        </bean>

</beans>

 

转载于:https://my.oschina.net/u/3832160/blog/2221903

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值