1. 程式人生 > >SpringMVC 學習 九 SSM環境搭建 (二) Spring配置檔案的編寫

SpringMVC 學習 九 SSM環境搭建 (二) Spring配置檔案的編寫

spring配置檔案中需要乾的事情

(一)開啟  Service與pojo包的註解掃描

注意:spring 掃描與表對應的實體類,以及service層的類,不能用來掃描Controller層的類,因為Controller層的類需要由SpringMVC容器來管理,如果採用了Spring容器管理,就會產生宣告式事物無效

<context:component-scan base-package="com.ssm.pojo,com.ssm.bean,com.ssm.service.impl"></context:component-scan>
    

(二)spring整合mybatis的一些配置

(1)載入properties配置檔案

<context:property-placeholder location="classpath:db.properties"/>

(2)配置資料來源

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
          <property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>

(3)配置sqlSessionFactory

 <!-- 配置sqlSessionFactory 
--> <!-- sqlSessionFactory ,註冊sqlSession工廠--> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--dataSource的型別是DataSource是一個介面,上面配置的DriverManagerDataSouce類實現了DataSource介面 --> <property name="dataSource" ref="dataSource"></property> <!-- 設定別名 --> <property name="typeAliasesPackage" value="com.ssm.pojo"></property> </bean>

(4)配置掃描器,用來掃描mybatis中xxxMapper.xml檔案以及xxxMapper.java介面檔案,並使用cglib的動態代理生成介面的實現類

 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.ssm.mapper" ></property>
            <!-- <property name="sqlSessionFactory" ref="factory"></property> -->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
      </bean>

(三)在開發中需要用到spring的宣告式事物,配置事物

(1)配置事物管理器

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

 

(2)配置事物

<!-- 配置宣告式事物,宣告式事物需要和Aop一起使用,實際工作中會開啟註解 -->
      <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <!-- 哪些方法需要有事務控制 -->
            <!-- 方法以 ins 開頭事務管理 -->
            <tx:method name="ins*" />
             <!-- 方法以 del 開頭事務管理 -->
            <tx:method name="del*" />
             <!-- 方法以 upd 開頭事務管理 -->
            <tx:method name="upd*" />
            <!-- readOnly
            事務屬性中的readOnly標誌表示對應的事務應該被最優化為只讀事務。
            如果值為true就會告訴Spring我這個方法裡面沒有insert或者update,
            你只需要提供只讀的資料庫Connection就行了,
            這種執行效率會比read-write的Connection高,
            所以這是一個最優化提示。
            在一些情況下,一些事務策略能夠起到顯著的最優化效果
            ,例如在使用Object/Relational對映工具
            (如:Hibernate或TopLink)時避免dirty checking(試圖“重新整理”)。 -->
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

(3)配置切點和切面,注意execution裡面的編寫方法,返回值要與方法全路徑之間有空格

<!-- 
            宣告式事物是基於AOP的,因此要宣告切點和通知
     -->
    <aop:config>
        <!-- 切點範圍設定大一些 -->
        <aop:pointcut expression="execution(* com.ssm.service.impl.*.*(..))" id="mypoint" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint" />
    
    </aop:config>

(4)cglib的配置,如果在程式碼中有需要把代理類物件賦值給目標類物件的程式碼,此時動態代理需要使用cglib的動態代理,如果沒有需要把代理物件賦值給目標物件,可以不用開啟cglib

<!-- 
        AOP的註解方式都是採用cglib動態代理
         proxy-target-class="true"代表採用cglib動態代理
         false代表使用jdk的動態代理
     -->
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>

 

配置檔案全文:

<?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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 開啟註解
        注意:spring 掃描與表對應的實體類,以及service層的類,不能用來掃描Controller層的類,
        因為Controller層的類需要由SpringMVC容器來管理,如果採用了Spring容器管理,
        就會產生宣告式事物無效
     -->
    <context:component-scan base-package="com.ssm.pojo,com.ssm.bean,com.ssm.service.impl"></context:component-scan>
    
    <!-- 載入屬性檔案 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置資料來源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
          <property name="driverClassName" value="${jdbc.driver}"></property>
          <property name="url" value="${jdbc.url}"></property>
          <property name="username" value="${jdbc.username}"></property>
          <property name="password" value="${jdbc.password}"></property>
    </bean>
    
    <!-- 配置sqlSessionFactory -->
    <!-- sqlSessionFactory ,註冊sqlSession工廠-->
     <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         <!--dataSource的型別是DataSource是一個介面,上面配置的DriverManagerDataSouce類實現了DataSource介面  -->
         <property name="dataSource" ref="dataSource"></property>
         <!-- 設定別名 -->
         <property name="typeAliasesPackage" value="com.ssm.pojo"></property>
     </bean>
    
    <!-- 掃描器,用來掃描mybatis中xxxMapper.xml檔案以及xxxMapper.java介面檔案,並使用cglib的動態代理生成介面的實現類 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.ssm.mapper" ></property>
            <!-- <property name="sqlSessionFactory" ref="factory"></property> -->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
      </bean>
      
      <!-- 配置事物管理器 -->
      <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="dataSource"></property>
      </bean>
      
      
      <!-- 配置宣告式事物,宣告式事物需要和Aop一起使用,實際工作中會開啟註解 -->
      <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <!-- 哪些方法需要有事務控制 -->
            <!-- 方法以 ins 開頭事務管理 -->
            <tx:method name="ins*" />
             <!-- 方法以 del 開頭事務管理 -->
            <tx:method name="del*" />
             <!-- 方法以 upd 開頭事務管理 -->
            <tx:method name="upd*" />
            <!-- readOnly
            事務屬性中的readOnly標誌表示對應的事務應該被最優化為只讀事務。
            如果值為true就會告訴Spring我這個方法裡面沒有insert或者update,
            你只需要提供只讀的資料庫Connection就行了,
            這種執行效率會比read-write的Connection高,
            所以這是一個最優化提示。
            在一些情況下,一些事務策略能夠起到顯著的最優化效果
            ,例如在使用Object/Relational對映工具
            (如:Hibernate或TopLink)時避免dirty checking(試圖“重新整理”)。 -->
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!-- 
            宣告式事物是基於AOP的,因此要宣告切點和通知
     -->
    <aop:config>
        <!-- 切點範圍設定大一些 -->
        <aop:pointcut expression="execution(* com.ssm.service.impl.*.*(..))" id="mypoint" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint" />
    
    </aop:config>
    <!-- 
        AOP的註解方式都是採用cglib動態代理
         proxy-target-class="true"代表採用cglib動態代理
         false代表使用jdk的動態代理
     -->
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
    
</beans>