<?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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    					http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    					http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.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-3.1.xsd
						http://www.springframework.org/schema/util	http://www.springframework.org/schema/util/spring-util-3.1.xsd
						http://www.springframework.org/schema/jee	http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
	<context:annotation-config />
	<context:component-scan base-package="com.wechat" />

	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!--1024*1024*500即500M--> 
		<property name="maxUploadSize" value="524288000"/>
		<property name="defaultEncoding" value="utf-8"/>
	</bean>
	
	<!-- 全局log -->
    <aop:aspectj-autoproxy/>

	
		
	<!-- 事务配置 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref local="dataSource" />
		</property>
	</bean>
	<bean id="transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager" ref="transactionManager"></property>
		<property name="transactionAttributes">
			<props>
				<prop key="get*">PROPAGATION_REQUIRED, readOnly</prop>
				<prop key="query*">PROPAGATION_REQUIRED, readOnly</prop>
				<prop key="find*">PROPAGATION_REQUIRED, readOnly</prop>
				<prop key="search*">PROPAGATION_REQUIRED, readOnly</prop>
				<prop key="list*">PROPAGATION_REQUIRED, readOnly</prop>
				<prop key="delOne*">PROPAGATION_REQUIRED, readOnly</prop>
				<prop key="*">PROPAGATION_REQUIRED , -Exception</prop>
			</props>
		</property>
	</bean>
	<!-- 基于注释的事务，当注释中发现@Transactional时，使用id为“transactionManager”的事务管理器  -->
    <!-- 如果没有设置transaction-manager的值，则spring以缺省默认的事务管理器来处理事务，默认事务管理器为第一个加载的事务管理器 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	
	<!-- mybatis配置 -->
	<!-- 配置SqlSessionFactoryBean -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="typeAliasesSuperType" value="com.wechat.model.dbEntity.BaseEntity"/>
		<!-- sql日志 -->
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<!-- mapper和resultmap配置路径 -->
		<property name="mapperLocations">
			<list>
				<value>classpath:mybatis/*.xml</value>
			</list>
		</property>
	</bean>
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory" />
	</bean>
	
	<!-- 扫描basePackage下所有以@MyBatisDao注解的接口 -->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <property name="basePackage" value="com.wechat.dao"/>
        <property name="annotationClass" value="com.wechat.global.base.annotation.MyBatisDao"/>
    </bean>
	
	<!-- message配置 -->
	<bean id="messageProperties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location">
			<value>classpath:message.tmpl.properties</value>
		</property>
    </bean>
	

	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="mybatis" />
	</bean>



	<!-- redis -->
	<bean class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory" >
		<!-- 指定redis使用String对key进行序列化防止出现乱码 -->
		<property name="keySerializer">  
	        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
	    </property>
	    <property name="hashKeySerializer">  
	        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
	    </property> 
    </bean>
    
	<beans profile="local">
		<!--  Redis Configuration -->
		<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
			<property name="maxIdle" value="#{configProperties['redis.pool.maxIdle']}" />
			<property name="testOnBorrow" value="#{configProperties['redis.pool.testOnBorrow']}" />
		</bean>

		<bean id="jedisConnectionFactory"
			class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
			<property name="hostName" value="#{configProperties['redis.ip']}" />
			<property name="port" value="#{configProperties['redis.port']}" />
			<property name="password" value="#{configProperties['redis.password']}" />
			<property name="database" value="#{configProperties['redis.database']}"/>
			<property name="poolConfig" ref="jedisPoolConfig" />
		</bean>
		
		<!-- 数据源配置 -->
		<bean
			class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
			<property name="location">
				<value>classpath:jdbc.local.properties</value>
			</property>
		</bean>
		<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
			<property name="driverClassName" value="${jdbc.mysql.driverClassName}"></property>
			<property name="url" value="${jdbc.mysql.url}"></property>
			<property name="username" value="${jdbc.mysql.username}"></property>
			<property name="password" value="${jdbc.mysql.password}"></property>
			<property name="timeBetweenEvictionRunsMillis" value="${jdbc.mysql.timeBetweenEvictionRunsMillis}"></property>
			<property name="minEvictableIdleTimeMillis" value="${jdbc.mysql.minEvictableIdleTimeMillis}"></property>
		</bean>
	
	    <bean id="configProperties"
	        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
	        <property name="locations">
	            <list>
	                <value>classpath*:*.local.properties</value>
	            </list>
	        </property>
	    </bean>
	</beans>
		
    <beans profile="test">
		<!--  Redis Configuration -->
		<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
			<property name="maxIdle" value="#{configProperties['redis.pool.maxIdle']}" />
			<property name="testOnBorrow" value="#{configProperties['redis.pool.testOnBorrow']}" />
		</bean>

		<bean id="jedisConnectionFactory"
			class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
			<property name="hostName" value="#{configProperties['redis.ip']}" />
			<property name="port" value="#{configProperties['redis.port']}" />
			<property name="password" value="#{configProperties['redis.password']}" />
			<property name="database" value="#{configProperties['redis.database']}"/>
			<property name="poolConfig" ref="jedisPoolConfig" />
		</bean>
		
		<!-- 数据源配置 -->
		<bean
			class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
			<property name="location">
				<value>classpath:jdbc.test.properties</value>
			</property>
		</bean>
		<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
			<property name="driverClassName" value="${jdbc.mysql.driverClassName}"></property>
			<property name="url" value="${jdbc.mysql.url}"></property>
			<property name="username" value="${jdbc.mysql.username}"></property>
			<property name="password" value="${jdbc.mysql.password}"></property>
			<property name="timeBetweenEvictionRunsMillis" value="${jdbc.mysql.timeBetweenEvictionRunsMillis}"></property>
			<property name="minEvictableIdleTimeMillis" value="${jdbc.mysql.minEvictableIdleTimeMillis}"></property>
		</bean>
	
	    <bean id="configProperties"
	        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
	        <property name="locations">
	            <list>
	                <value>classpath*:*.test.properties</value>
	            </list>
	        </property>
	    </bean>
	</beans>

    <beans profile="prod">
		<!--  Redis Configuration -->
		<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
			<property name="maxIdle" value="#{configProperties['redis.pool.maxIdle']}" />
			<property name="testOnBorrow" value="#{configProperties['redis.pool.testOnBorrow']}" />
		</bean>

		<bean id="jedisConnectionFactory"
			class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
			<property name="hostName" value="#{configProperties['redis.ip']}" />
			<property name="port" value="#{configProperties['redis.port']}" />
			<property name="password" value="#{configProperties['redis.password']}" />
			<property name="database" value="#{configProperties['redis.database']}"/>
			<property name="poolConfig" ref="jedisPoolConfig" />
		</bean>
		
		<!-- 数据源配置 -->
		<bean
			class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
			<property name="location">
				<value>classpath:jdbc.prod.properties</value>
			</property>
		</bean>
		<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
			<property name="driverClassName" value="${jdbc.mysql.driverClassName}"></property>
			<property name="url" value="${jdbc.mysql.url}"></property>
			<property name="username" value="${jdbc.mysql.username}"></property>
			<property name="password" value="${jdbc.mysql.password}"></property>
			<property name="timeBetweenEvictionRunsMillis" value="${jdbc.mysql.timeBetweenEvictionRunsMillis}"></property>
			<property name="minEvictableIdleTimeMillis" value="${jdbc.mysql.minEvictableIdleTimeMillis}"></property>
		</bean>
		
	    <bean id="configProperties"
	        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
	        <property name="locations">
	            <list>
	                <value>classpath*:*.prod.properties</value>
	            </list>
	        </property>
	    </bean>
	</beans>

</beans>