spring知識點詳解

一、spring命名空間

Spring中所有的命名空間:
下載Spring源文件,解壓後會有個schema目錄,裏面是所有Spring模塊。如下圖:





二、事物配置詳解

1、全註解xml文件配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="dataSourceMaster" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <property name="driverClassName" value="${master.jdbc.driverClass}"/>
        <property name="url" value="${master.jdbc.url}"/>
        <property name="username" value="${master.jdbc.user}"/>
        <property name="password" value="${master.jdbc.password}"/>
        <property name="filters" value="stat"/>
        <property name="maxActive" value="200"/>
        <property name="initialSize" value="0"/>
        <property name="maxWait" value="60000"/>
        <property name="minIdle" value="0"/>
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>
        <property name="minEvictableIdleTimeMillis" value="300000"/>
        <property name="validationQuery" value="SELECT 'x'"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>
    </bean>

    <bean id="masterSqlSessionFactory"
          class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSourceMaster"/>
        <property name="configLocation"
                  value="classpath:/mybatis/mybatis_config.xml"/>
        <property name="mapperLocations" value="classpath:mybatis/*Mapper.xml"/>
    </bean>

	<!-- mybatis mappers, scanned automatically -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dao.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="masterSqlSessionFactory"/>
    </bean>

    <bean id="sqlSessionMaster" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
        <constructor-arg index="0" ref="masterSqlSessionFactory"/>
    </bean>
    
    <!-- spring開啓事務控制的註解支持 --> 
    <!-- MyBatis自動參與到spring事務管理中,無需額外配置,
	只要org.mybatis.spring.SqlSessionFactoryBean引用的數據源
	與DataSourceTransactionManager引用的數據源一致即可,否則事務管理會不起作用。-->  
    <tx:annotation-driven transaction-manager="transactionManagerMaster" />

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章