applicationContext.xml 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:jee="http://www.springframework.org/schema/jee"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  11. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  12. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  13. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
  14. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  15. <!-- <context:property-placeholder location="classpath:jdbc.properties" /> -->
  16. <!-- 设置需要进行Spring注解扫描的类包 -->
  17. <context:component-scan base-package="com.happy" />
  18. <!-- session设置 -->
  19. <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
  20. <property name="maxInactiveIntervalInSeconds" value="3600"></property>
  21. </bean>
  22. <!-- redis连接池 -->
  23. <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"></bean>
  24. <!-- redis连接工厂 -->
  25. <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  26. <property name="hostName" value="106.12.61.111"/>
  27. <property name="port" value="6379"/>
  28. <property name="password" value="Asd_123!"/>
  29. <property name="timeout" value="20000"/>
  30. <property name="poolConfig" ref="poolConfig"></property>
  31. </bean>
  32. <!-- <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  33. <property name="connectionFactory" ref="connectionFactory"/>
  34. </bean> -->
  35. <!-- 自动加载构建bean
  36. <context:component-scan base-package="com.happy.service" />
  37. <context:component-scan base-package="com.happy.dao" /> -->
  38. <!-- 写库数据源 -->
  39. <bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  40. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  41. <property name="url" value="jdbc:mysql://106.12.61.111:3306/HotWater?useCursorFetch=true&amp;autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"></property>
  42. <property name="username" value="root"></property>
  43. <property name="password" value="Asd!688549"></property>
  44. <!-- 初始化连接大小 -->
  45. <property name="initialSize" value="1" />
  46. <!-- 连接池最大使用连接数量 -->
  47. <property name="maxActive" value="201" />
  48. <!-- 连接池最小空闲 -->
  49. <property name="minIdle" value="1" />
  50. <!-- 配置获取连接等待超时的时间 -->
  51. <property name="maxWait" value="60000" />
  52. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  53. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  54. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  55. <property name="minEvictableIdleTimeMillis" value="300000" />
  56. <property name="validationQuery" value="SELECT 'x'" />
  57. <property name="testWhileIdle" value="true" />
  58. <property name="testOnBorrow" value="false" />
  59. <property name="testOnReturn" value="false" />
  60. <!-- 超过时间限制是否回收 -->
  61. <!-- <property name="removeAbandoned" value="true" /> -->
  62. <!-- 超时时间;单位为秒。180秒=3分钟 -->
  63. <!-- <property name="removeAbandonedTimeout" value="180" /> -->
  64. <!-- 关闭abanded连接时输出错误日志 -->
  65. <!-- <property name="logAbandoned" value="true" /> -->
  66. <!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle使用) -->
  67. <!-- <property name="poolPreparedStatements" value="true" />
  68. <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> -->
  69. <!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
  70. <property name="filters" value="stat" />
  71. </bean>
  72. <!-- 读库数据源 -->
  73. <bean id="slaveDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  74. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  75. <property name="url" value="jdbc:mysql://106.12.61.111:3306/HotWater?useCursorFetch=true&amp;autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"></property>
  76. <property name="username" value="root"></property>
  77. <property name="password" value="Asd!688549"></property>
  78. <!-- 初始化连接大小 -->
  79. <property name="initialSize" value="1" />
  80. <!-- 连接池最大使用连接数量 -->
  81. <property name="maxActive" value="201" />
  82. <!-- 连接池最小空闲 -->
  83. <property name="minIdle" value="1" />
  84. <!-- 配置获取连接等待超时的时间 -->
  85. <property name="maxWait" value="60000" />
  86. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  87. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  88. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  89. <property name="minEvictableIdleTimeMillis" value="300000" />
  90. <property name="validationQuery" value="SELECT 'x'" />
  91. <property name="testWhileIdle" value="true" />
  92. <property name="testOnBorrow" value="false" />
  93. <property name="testOnReturn" value="false" />
  94. <!-- 超过时间限制是否回收 -->
  95. <!-- <property name="removeAbandoned" value="true" /> -->
  96. <!-- 超时时间;单位为秒。180秒=3分钟 -->
  97. <!-- <property name="removeAbandonedTimeout" value="180" /> -->
  98. <!-- 关闭abanded连接时输出错误日志 -->
  99. <!-- <property name="logAbandoned" value="true" /> -->
  100. <!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle使用) -->
  101. <!-- <property name="poolPreparedStatements" value="true" />
  102. <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> -->
  103. <!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
  104. <property name="filters" value="stat" />
  105. </bean>
  106. <!-- 定义数据源,使用自己定义的数据源 -->
  107. <bean id="dataSource" class="com.happy.myDataSource.DynamicDatasource">
  108. <!-- 设置多个数据源 -->
  109. <property name="targetDataSources">
  110. <map key-type="java.lang.String">
  111. <entry key="master" value-ref="masterDataSource"></entry>
  112. <entry key="slave" value-ref="slaveDataSource"></entry>
  113. </map>
  114. </property>
  115. <property name="defaultTargetDataSource" ref="masterDataSource"></property>
  116. </bean>
  117. <!-- 配置Jdbc模板 -->
  118. <!-- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  119. <property name="dataSource" ref="dataSource"></property>
  120. </bean> -->
  121. <!-- 配置Jdbc模板 -->
  122. <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" scope="prototype">
  123. <constructor-arg ref="dataSource"></constructor-arg>
  124. </bean>
  125. <!-- 配置事务管理器 -->
  126. <bean id="transactionManager" scope="prototype"
  127. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  128. <property name="dataSource" ref="dataSource" />
  129. </bean>
  130. <!-- 配置事务通知属性 -->
  131. <tx:advice id="txAdvice" transaction-manager="transactionManager" >
  132. <!-- 定义事务传播属性 -->
  133. <tx:attributes>
  134. <tx:method name="insert*" propagation="REQUIRED" />
  135. <tx:method name="count*" propagation="REQUIRED" />
  136. <tx:method name="update*" propagation="REQUIRED" />
  137. <tx:method name="edit*" propagation="REQUIRED" />
  138. <tx:method name="save*" propagation="REQUIRED" />
  139. <tx:method name="add*" propagation="REQUIRED" />
  140. <tx:method name="new*" propagation="REQUIRED" />
  141. <tx:method name="set*" propagation="REQUIRED" />
  142. <tx:method name="remove*" propagation="REQUIRED" />
  143. <tx:method name="delete*" propagation="REQUIRED"/>
  144. <tx:method name="change*" propagation="REQUIRED" />
  145. <tx:method name="batch*" propagation="REQUIRED" />
  146. <tx:method name="get*" read-only="true" />
  147. <tx:method name="find*" read-only="true" />
  148. <tx:method name="load*" read-only="true" />
  149. <tx:method name="comb*" read-only="true" />
  150. <tx:method name="query*" read-only="true" />
  151. <tx:method name="list*" read-only="true" />
  152. <tx:method name="export*" read-only="true" />
  153. <tx:method name="*" propagation="REQUIRED" />
  154. </tx:attributes>
  155. </tx:advice>
  156. <bean id="dataSourceAspect" class="com.happy.myDataSource.DataSourceAspect"></bean>
  157. <!-- 配置事务切面 -->
  158. <aop:config>
  159. <!-- 配置切点 -->
  160. <aop:pointcut id="serviceOperation"
  161. expression="execution(* com.happy.service.*.*(..))" />
  162. <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
  163. <aop:aspect ref="dataSourceAspect" order="-9999">
  164. <aop:before method="before" pointcut-ref="serviceOperation"/>
  165. </aop:aspect>
  166. </aop:config>
  167. </beans>