springMVC對ibatis,hibernate,aop,緩存等的配置

jopen 11年前發布 | 36K 次閱讀 Spring MVC Web框架 SpringMVC

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>springmvc3</display-name>
   <servlet> 
        <servlet-name>dispatcher</servlet-name> 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
        <init-param> 
            <description>指定配置文件路徑</description> 
            <param-name>contextConfigLocation</param-name> 
            <param-value>/WEB-INF/applicationContext.xml</param-value> 
        </init-param> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
        <servlet-name>dispatcher</servlet-name> 
        <url-pattern>*.do</url-pattern> 
    </servlet-mapping> 

    <filter> 
        <filter-name>encoding-filter</filter-name> 
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
        <init-param> 
            <param-name>encoding</param-name> 
            <param-value>UTF-8</param-value> 
        </init-param> 
        <init-param> 
            <param-name>forceEncoding</param-name> 
            <param-value>true</param-value> 
        </init-param> 
    </filter> 
    <filter-mapping> 
        <filter-name>encoding-filter</filter-name> 
        <url-pattern>*.do</url-pattern> 
    </filter-mapping> 

  <error-page>
      <error-code>400</error-code>
      <location>/error.jsp</location>
  </error-page>
  <error-page>
      <error-code>404</error-code>
      <location>/error.jsp</location>
  </error-page>
  <error-page>
      <exception-type>java.lang.Exception</exception-type>
      <location>/error.jsp</location>
  </error-page>

  <welcome-file-list>
    <welcome-file>/index.do</welcome-file>
  </welcome-file-list>
</web-app>


applicationContext.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:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" 
    xsi:schemaLocation=" 
        http://www.springframework.org/schema/aop 
            classpath:/org/springframework/aop/config/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/beans
            classpath:/org/springframework/beans/factory/xml/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/context 
            classpath:/org/springframework/context/config/spring-context-3.0.xsd 
        http://www.springframework.org/schema/mvc 
            classpath:/org/springframework/web/servlet/config/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/tx 
            classpath:/org/springframework/transaction/config/spring-tx-3.0.xsd 
        http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring 
            classpath:/com/googlecode/ehcache/annotations/ehcache-spring-1.1.xsd 
    "> 

    <!-- ===================================== 視圖配置 ===================================== --> 
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/jsp/"/> 
        <property name="suffix" value=".jsp"/> 
    </bean> 

    <!-- ===================================== 注解驅動的配置 ===================================== --> 
    <!-- <mvc:annotation-driven />  -->
    <context:component-scan base-package="com.gdie.forum" /> 

    <!-- ===================================== 數據源和事務管理 ===================================== --> 
    <bean id="dataSource" 
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="dm.jdbc.driver.DmDriver" /> 
        <property name="url" value="jdbc:dm://192.168.20.65:5236" /> 
        <property name="username" value="user" /> 
        <property name="password" value="user" /> 
    </bean> 

    <bean id="transactionManager" 
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
        <property name="dataSource" ref="dataSource" /> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager" 
        proxy-target-class="true" /> 
    <!--使用方式:在需要進行事務管理的方法上添加@Transactional(rollbackFor = Exception.class)-->      

    <!-- ===================================== mybatis ===================================== --> 
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource" /> 
        <!--property name="configLocation" value="/WEB-INF/Mybatis-Configuration.xml" /--> 
        <property name="mapperLocations"> 
            <list> 
                <value>classpath:com/gdie/forum/dataaccess/mybatis/dm/*.xml</value> 
            </list> 
        </property> 
    </bean> 
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 
        <constructor-arg index="0" ref="sqlSessionFactory" /> 
    </bean> 

    <!-- ===================================== AOP設置 ===================================== >
    <aop:config>
        <aop:aspect id="logAOP" ref="logAop">
            <aop:pointcut expression="execution(* com.gdie.mvcdemo.controllers.*.*(..))" id="target"/>
            <aop:before method="methodTrace" pointcut-ref="target"/>
            <aop:around method="timeCost" pointcut-ref="target"/>
        </aop:aspect>
    </aop:config-->

    <!-- ===================================== 攔截器 ===================================== -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/forum/*.do" />
            <mvc:mapping path="/thread/*.do"/>
            <mvc:mapping path="/index.do"/>
            <bean class="com.gdie.forum.interceptors.LoginInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>

    <!-- ===================================== cache設置 ===================================== -->
    <ehcache:annotation-driven />
    <ehcache:config cache-manager="cacheManager">
        <ehcache:evict-expired-elements interval="60" />
    </ehcache:config>
    <bean id="cacheManager"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="/WEB-INF/ehcache.xml" />
    </bean>
    <!--使用方式:在得到需要緩存數據的方法上:@Cacheable(cacheName=Const.FORUM_CACHE)
        在進行添加、刪除、更新時,需要刪除緩存,否則顯示的結果沒有變化
        @TriggersRemove(cacheName=Const.FORUM_CACHE, removeAll=true)
        其中Const.FORUM_CACHE在緩存配置文件ehcache.xml中配置
        Const為自定義的存放靜態變量的類
    -->
    <!-- ===================================== Hibernate設置 ===================================== -->
<!-- 定義Hibernate的SessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref local="dataSource" />
        </property>
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath:com/lzw/model</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.SQLServerDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
    <!-- 定義Hibernate的事務管理器 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>
    <!--定義一個事物通知txAdvice,配置事物的傳播特性-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
        <!--所有以browse\list\load\get\is開頭的業務邏輯方法均不需要事物控制且只讀-->
            <tx:method name="browse*" propagation="NOT_SUPPORTED" read-only="true"/>
            <tx:method name="list*" propagation="NOT_SUPPORTED" read-only="true"/>
            <tx:method name="load*" propagation="NOT_SUPPORTED" read-only="true"/>
            <tx:method name="get*" propagation="NOT_SUPPORTED" read-only="true"/>
            <tx:method name="is*" propagation="NOT_SUPPORTED" read-only="true"/>
            <!--設置所有方法均進行事物控制,如果當前沒有事物,則新建一個事物-->
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--基于AOP技術的事物管理實現-->
    <aop:config>
    <!--定義一個事務切入點,攔截com.company.service.impl包中所有類的所有方法-->
        <aop:pointcut id="transactionPointcut" expression="execution(* com.company.service.impl.*.*(..))"/>
        <!--引用txAdvice事務通知-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
    </aop:config>
</beans> 


ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="true" monitoring="autodetect"
         dynamicConfig="true">

   <diskStore path="java.io.tmpdir"/>
       <transactionManagerLookup class="net.sf.ehcache.transaction.manager.DefaultTransactionManagerLookup"
                              properties="jndiName=java:/TransactionManager" propertySeparator=";"/>

  <cacheManagerEventListenerFactory class="" properties=""/>
     <cacheManagerPeerProviderFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
            properties="peerDiscovery=automatic,
                        multicastGroupAddress=230.0.0.1,
                        multicastGroupPort=4446, timeToLive=1"
            propertySeparator=","
            />
  <cacheManagerPeerListenerFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"/>
 <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            diskSpoolBufferSizeMB="30"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            statistics="false"
            />
    <cache name="forumCache"
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            diskSpoolBufferSizeMB="30"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            statistics="false"
            />
          <cacheEventListenerFactory
                class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
        <bootstrapCacheLoaderFactory
                class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>
    </cache>
</ehcache>


 

 

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!