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

	<!-- ====================================================================================== -->
	<!-- 启用基于注解（Annotation-based）的配置 -->
	<!-- ====================================================================================== -->
	<context:annotation-config />
	
	<!-- ====================================================================================== -->
	<!-- 加载属性文件 -->
	<!-- ====================================================================================== -->
	<context:property-placeholder location="classpath*:*.properties" />
	
	<!-- ====================================================================================== -->
	<!-- 多数据源配置 -->
	<!-- ====================================================================================== -->
	<bean id="multipleDataSource" class="com.ylcm.sys.multiple.MultipleDataSource" primary="true">
		<property name="targetDataSources">
			<map key-type="java.lang.String">
				<entry value-ref="dataSource" key="dataSource"></entry>
				<entry value-ref="readOnlyDataSource" key="readOnlyDataSource"></entry>
			</map>
		</property>
		<property name="defaultTargetDataSource" ref="dataSource"/>
	</bean>
	
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<!-- 初始化连接数量 -->
		<property name="initialSize" value="${initSize}"/>
        <!-- 最大并发连接数 -->
		<property name="maxActive" value="${maxSize}"/>
        <!-- 配置获取连接等待超时的时间 -->     
        <property name="maxWait" value="60000" />
        <!-- 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- 配置一个连接在池中最小生存的时间，单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        <!-- 对于长时间不使用的连接强制关闭 -->  
        <property name="removeAbandoned" value="true" />  
        <!-- 关闭超过30分钟的空闲连接，1800秒，也就是30分钟 -->  
        <property name="removeAbandonedTimeout" value="1800" />  
        
        <!-- 用来检测连接是否有效的sql，要求是一个查询语句-->
        <property name="validationQuery" value="select 1" />
        <!-- 申请连接的时候检测 -->
        <property name="testWhileIdle" value="true" />
        <!-- 归还连接时执行validationQuery检测连接是否有效，配置为true会降低性能  -->
        <!-- <property name="testOnReturn" value="${druid.testOnReturn}" /> -->
        <!-- 打开PSCache，并且指定每个连接上PSCache的大小 -->
        <property name="poolPreparedStatements" value="true" />     
        <property name="maxPoolPreparedStatementPerConnectionSize" value="${psCacheSize}" />
        <!-- 对数据库密码进行加密 -->
        <property name="connectionProperties" value="config.decrypt=true" />
        <!--属性类型是字符串，通过别名的方式配置扩展插件，常用的插件有：   监控统计用的filter:stat(加密时使用config), 日志用的filter:log4j, 防御SQL注入的filter:wall -->
        <property name="filters" value="config,log4j" />
	</bean>
	
	<!-- 只读数据库 -->
	<bean id="readOnlyDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.readonly.driver}" />
		<property name="url" value="${jdbc.readonly.url}" />
		<property name="username" value="${jdbc.readonly.username}" />
		<property name="password" value="${jdbc.readonly.password}" />
		<!-- 初始化连接数量 -->
		<property name="initialSize" value="${initSize}"/>
        <!-- 最大并发连接数 -->
		<property name="maxActive" value="${maxSize}"/>
        <!-- 配置获取连接等待超时的时间 -->     
        <property name="maxWait" value="60000" />
        <!-- 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- 配置一个连接在池中最小生存的时间，单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        
        <!-- 对于长时间不使用的连接强制关闭 -->  
        <property name="removeAbandoned" value="true" />  
        <!-- 关闭超过30分钟的空闲连接，1800秒，也就是30分钟 -->  
        <property name="removeAbandonedTimeout" value="1800" />
        
        <!-- 用来检测连接是否有效的sql，要求是一个查询语句-->
        <property name="validationQuery" value="select 1" />
        <!-- 申请连接的时候检测 -->
        <property name="testWhileIdle" value="true" />
        <!-- 归还连接时执行validationQuery检测连接是否有效，配置为true会降低性能  -->
        <!-- <property name="testOnReturn" value="${druid.testOnReturn}" /> -->
        <!-- 打开PSCache，并且指定每个连接上PSCache的大小 -->
        <property name="poolPreparedStatements" value="true" />     
        <property name="maxPoolPreparedStatementPerConnectionSize" value="${psCacheSize}" />
        <!-- 对数据库密码进行加密 -->
        <property name="connectionProperties" value="config.decrypt=true" />
        <!--属性类型是字符串，通过别名的方式配置扩展插件，常用的插件有：   监控统计用的filter:stat(加密时使用config), 日志用的filter:log4j, 防御SQL注入的filter:wall -->
        <property name="filters" value="config,log4j" />
	</bean>

	<!-- ====================================================================================== -->
	<!-- 配置 SqlMapClient -->
	<!-- ====================================================================================== -->
<!-- 	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> -->
<!-- 		<property name="dataSource" ref="multipleDataSource" /> -->
<!-- 		<property name="mapperLocations"> -->
<!-- 			<list> -->
<!-- 				<value>classpath:mapper/*Mapper.xml</value> -->
<!-- 			</list> -->
<!-- 		</property> -->
<!-- 		<property name="configLocation" value="classpath:mybatisConfig.xml" /> -->
<!-- 	</bean> -->
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.ylcm.sys.mapper"></property>
	</bean>
	
	<!-- mybatisplus -->
	<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
		<property name="dataSource" ref="multipleDataSource" />
		<property name="mapperLocations">
			<list>
				<value>classpath:mapper/*Mapper.xml</value>
			</list>
		</property>
		<property name="globalConfig" ref="globalConfig"/>
<!-- 		<property name="configLocation" value="classpath:mybatisConfig.xml" /> -->
		<property name="plugins">
		    <array>
		        <bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
<!-- 		            <property name="sqlParser" ref="自定义解析类、可以没有"/> -->
<!-- 		            <property name="dialectClazz" value="自定义方言类、可以没有"/> -->
		            <!-- COUNT SQL 解析.可以没有 -->
<!-- 		            <property name="countSqlParser" ref="countSqlParser"/> -->
		        </bean>
		    </array>
		</property>
	</bean>
	
	<bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
	    <property name="dbConfig" ref="dbConfig"/> <!--  非必须  -->
	</bean>
	
	<bean id="dbConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig">
	    <property name="logicDeleteValue" value="4"></property>
        <property name="logicNotDeleteValue" value="0"></property>
	</bean>
	

	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- set the max upload size100MB -->
		<property name="maxUploadSize">
			<value>52428800</value>
		</property>
		<property name="maxInMemorySize">
			<value>4096</value>
		</property>
	</bean>
	<!-- 添加事务传播 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="multipleDataSource" />
	</bean>
	<!-- 开启事物注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="append*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="modify*" propagation="REQUIRED" />
			<tx:method name="edit*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />
			<tx:method name="repair" propagation="REQUIRED" />
			<tx:method name="delAndRepair" propagation="REQUIRED" />

			<tx:method name="get*" propagation="SUPPORTS" />
			<tx:method name="find*" propagation="SUPPORTS" />
			<tx:method name="load*" propagation="SUPPORTS" />
			<tx:method name="search*" propagation="SUPPORTS" />
			<tx:method name="datagrid*" propagation="SUPPORTS" />

			<tx:method name="*" propagation="SUPPORTS" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="transactionPointcut" expression="execution(* com.ylcm.sys.service.impl..*Impl.*(..))" />
		<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
	</aop:config>
	<!-- 自定义 -->
	<aop:aspectj-autoproxy/>
</beans>