<?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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:p="http://www.springframework.org/schema/p"
       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/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
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
       default-lazy-init="true">

    <description>Spring公共配置</description>
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <context:component-scan base-package="tkio"/>
    <context:component-scan base-package="track"/>
    <context:component-scan base-package="userio"/>
    <context:component-scan base-package="common"/>
    <context:component-scan base-package="office"/>
    <context:component-scan base-package="dmp"/>
    <context:component-scan base-package="security"/>
    <context:component-scan base-package="adi"/>

    <context:property-placeholder location="classpath:persistence.properties,classpath:redis.properties"/>

    <!--Redis配置-->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="300"/>
        <property name="maxTotal" value="600"/>
        <property name="maxWaitMillis" value="1000"></property>
        <property name="testOnBorrow" value="true"/>
    </bean>

    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:hostName="${redis.Surl.host}" p:port="${redis.Surl.port}" p:poolConfig-ref="poolConfig"/>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
    </bean>
    <!--//Redis配置 -->

    <bean id="parentDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${dataSource.driverClassName}"/>

        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="1"/>
        <property name="minIdle" value="1"/>
        <property name="maxActive" value="20"/>

        <!-- 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>

        <!-- 配置一个连接在池中最小生存的时间，单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000"/>
    </bean>

    <bean id="dataSource" parent="parentDataSource">
        <property name="url" value="${default.dataSource.url}"/>
        <property name="username" value="${default.dataSource.username}"/>
        <property name="password" value="${default.dataSource.password}"/>
    </bean>
    <bean id="officeDataSource" parent="parentDataSource">
        <property name="url" value="${office.dataSource.url}"/>
        <property name="username" value="${office.dataSource.username}"/>
        <property name="password" value="${office.dataSource.password}"/>
    </bean>
    <bean id="tkioDataSource" parent="parentDataSource">
        <property name="url" value="${tkio.dataSource.url}"/>
        <property name="username" value="${tkio.dataSource.username}"/>
        <property name="password" value="${tkio.dataSource.password}"/>
    </bean>
    <bean id="trackDataSource" parent="parentDataSource">
        <property name="url" value="${track.dataSource.url}"/>
        <property name="username" value="${track.dataSource.username}"/>
        <property name="password" value="${track.dataSource.password}"/>
    </bean>
    <bean id="dmpDataSource" parent="parentDataSource">
        <property name="url" value="${dmp.dataSource.url}"/>
        <property name="username" value="${dmp.dataSource.username}"/>
        <property name="password" value="${dmp.dataSource.password}"/>
    </bean>
    <bean id="ioDataSource" parent="parentDataSource">
        <property name="url" value="${io.dataSource.url}"/>
        <property name="username" value="${io.dataSource.username}"/>
        <property name="password" value="${io.dataSource.password}"/>
    </bean>

    <!-- JPA实体管理工厂的配置 -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="common.model">
        </property>
        <property name="persistenceUnitName" value="defaultUnit"/>

        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop><!-- none -->

                <prop key="hibernate.connection.CharSet">utf8</prop>
                <prop key="hibernate.connection.characterEncoding">utf8</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
            </props>
        </property>
    </bean>
    <bean id="officeEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="officeDataSource"/>
        <property name="packagesToScan" value="office.model"></property>
        <property name="persistenceUnitName" value="officeUnit"/>

        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop><!-- none -->

                <prop key="hibernate.connection.CharSet">utf8</prop>
                <prop key="hibernate.connection.characterEncoding">utf8</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
            </props>
        </property>
    </bean>
    <bean id="tkioEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="tkioDataSource"/>
        <property name="packagesToScan" value="tkio.model"></property>
        <property name="persistenceUnitName" value="tkioUnit"/>

        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop><!-- none -->

                <prop key="hibernate.connection.CharSet">utf8</prop>
                <prop key="hibernate.connection.characterEncoding">utf8</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
            </props>
        </property>
    </bean>
    <bean id="trackEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="trackDataSource"/>
        <property name="packagesToScan" value="track.model"></property>
        <property name="persistenceUnitName" value="tkrackUnit"/>

        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop><!-- none -->

                <prop key="hibernate.connection.CharSet">utf8</prop>
                <prop key="hibernate.connection.characterEncoding">utf8</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
            </props>
        </property>
    </bean>
    <bean id="dmpEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dmpDataSource"/>
        <property name="packagesToScan" value="dmp.model"></property>
        <property name="persistenceUnitName" value="dmpUnit"/>

        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop><!-- none -->

                <prop key="hibernate.connection.CharSet">utf8</prop>
                <prop key="hibernate.connection.characterEncoding">utf8</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
            </props>
        </property>
    </bean>
    <bean id="ioEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="ioDataSource"/>
        <property name="packagesToScan" value="userio.model"></property>
        <property name="persistenceUnitName" value="ioUnit"/>

        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop><!-- none -->

                <prop key="hibernate.connection.CharSet">utf8</prop>
                <prop key="hibernate.connection.characterEncoding">utf8</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
            </props>
        </property>
    </bean>

    <!--指定实现JPA的适配器 -->
    <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
    </bean>

    <!-- Jpa 事务配置 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <bean id="officeTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="officeEntityManagerFactory"/>
    </bean>
    <bean id="tkioTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="tkioEntityManagerFactory"/>
    </bean>
    <bean id="trackTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="trackEntityManagerFactory"/>
    </bean>
    <bean id="dmpTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="dmpEntityManagerFactory"/>
    </bean>
    <bean id="ioTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="ioEntityManagerFactory"/>
    </bean>

    <!-- Spring Data Jpa配置 -->
    <jpa:repositories base-package="common.repository"
                      repository-impl-postfix="Impl" transaction-manager-ref="transactionManager"
                      entity-manager-factory-ref="entityManagerFactory"/>
    <jpa:repositories base-package="office.repository"
                      repository-impl-postfix="Impl" transaction-manager-ref="officeTransactionManager"
                      entity-manager-factory-ref="officeEntityManagerFactory"/>
    <jpa:repositories base-package="tkio.repository"
                      repository-impl-postfix="Impl" transaction-manager-ref="tkioTransactionManager"
                      entity-manager-factory-ref="tkioEntityManagerFactory"/>
    <jpa:repositories base-package="track.repository"
                      repository-impl-postfix="Impl" transaction-manager-ref="trackTransactionManager"
                      entity-manager-factory-ref="trackEntityManagerFactory"/>
    <jpa:repositories base-package="dmp.repository"
                      repository-impl-postfix="Impl" transaction-manager-ref="dmpTransactionManager"
                      entity-manager-factory-ref="dmpEntityManagerFactory"/>
    <jpa:repositories base-package="userio.repository"
                      repository-impl-postfix="Impl" transaction-manager-ref="ioTransactionManager"
                      entity-manager-factory-ref="ioEntityManagerFactory"/>


    <!-- 使用annotation定义事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
    <tx:annotation-driven transaction-manager="officeTransactionManager" proxy-target-class="true"/>
    <tx:annotation-driven transaction-manager="tkioTransactionManager" proxy-target-class="true"/>
    <tx:annotation-driven transaction-manager="trackTransactionManager" proxy-target-class="true"/>
    <tx:annotation-driven transaction-manager="dmpTransactionManager" proxy-target-class="true"/>
    <tx:annotation-driven transaction-manager="ioTransactionManager" proxy-target-class="true"/>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="report*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="list*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <tx:advice id="officeTxAdvice" transaction-manager="officeTransactionManager">
        <tx:attributes>
            <tx:method name="report*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="list*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <tx:advice id="tkioTxAdvice" transaction-manager="tkioTransactionManager">
        <tx:attributes>
            <tx:method name="report*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="list*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <tx:advice id="trackTxAdvice" transaction-manager="trackTransactionManager">
        <tx:attributes>
            <tx:method name="report*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="list*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <tx:advice id="dmpTxAdvice" transaction-manager="dmpTransactionManager">
        <tx:attributes>
            <tx:method name="report*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="list*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <tx:advice id="ioTxAdvice" transaction-manager="ioTransactionManager">
        <tx:attributes>
            <tx:method name="report*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="list*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <aop:config expose-proxy="true">
        <aop:pointcut id="txPointcut" expression="execution(* common.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>
    <aop:config expose-proxy="true">
        <aop:pointcut id="officetxPointcut" expression="execution(* office.service.*.*(..))"/>
        <aop:advisor advice-ref="officeTxAdvice" pointcut-ref="officetxPointcut"/>
    </aop:config>
    <aop:config expose-proxy="true">
        <aop:pointcut id="tkiotxPointcut" expression="execution(* tkio.service.*.*(..))"/>
        <aop:advisor advice-ref="tkioTxAdvice" pointcut-ref="tkiotxPointcut"/>
    </aop:config>
    <aop:config expose-proxy="true">
        <aop:pointcut id="tracktxPointcut" expression="execution(* track.service.*.*(..))"/>
        <aop:advisor advice-ref="trackTxAdvice" pointcut-ref="tracktxPointcut"/>
    </aop:config>
    <aop:config expose-proxy="true">
        <aop:pointcut id="iotxPointcut" expression="execution(* userio.service.*.*(..))"/>
        <aop:advisor advice-ref="ioTxAdvice" pointcut-ref="iotxPointcut"/>
    </aop:config>

    <!-- SpringMVC上传文件时，需要配置MultipartResolver处理器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>
        <!-- 指定所上传文件的总大小不能超过2000KB。注意maxUploadSize属性的限制不是针对单个文件，而是所有文件的容量之和 -->
        <property name="maxUploadSize" value="2000000"/>
    </bean>

    <bean id="appUtils" class="common.context.AppUtils" lazy-init="false"/>
</beans>
