springboot为什么使用exclude注解?什么是多数据源?
为什么使用exclude注解?
什么是双(多)数据源?
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd" > <import resource="classpath*:/test1-core-spring/test1-datasource.xml"/> <context:component-scan base-package="com.yunpeng.core.test1"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan> //这里扫码第二套数据源相关的业务类 <context:component-scan base-package="com.yunpeng.core.test2"/> //这里加载第二套数据源相关的数据源配置 <import resource="classpath*:test2-core-spring/test2-core-dao.xml"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager"/> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 使用 annotation 定义事务 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <!-- MyBatis 配置 --> //重点这里已经有了一个名字叫sqlSessionFactory的bean了,并且只扫描了 //com.yunpeng.core.test1.dao下面的接口,如果当前文件引入的第二套中也有一个叫 //sqlSessionFactory的bean,会把第二套中的覆盖掉,于是只剩下了第一套的 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="typeAliasesPackage" value="com.yunpeng.core.test1.entity,com.yunpeng.core.test1.enumtype"/> <property name="mapperLocations" value="classpath:/test1-process-mapper/*Mapper.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.yunpeng.core.test1.dao"/> <property name="annotationClass" value="org.springframework.stereotype.Repository"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>
这是第二套数据源的相关配置,重点是sqlSessionFactory这个bean一定不要和第一个的重复。
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-autowire="byName"> <import resource="classpath*:/test2-core-spring/test2-core-datasource.xml"/> <bean id="txManagerTest2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="test2DS" /> <property name="nestedTransactionAllowed" value="true"></property> </bean> <tx:annotation-driven transaction-manager="txManagerTest2" proxy-target-class="true" /> <bean id="transactionTemplateTest2" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="txManagerTest2" /> </bean> <bean id="jdbcTemplateTest2" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="test2DS" /> </bean> //这里的bean一定要与数据源一中的区别开来,否则会被第一套覆盖,这是我加了Test2区分 <bean id="sqlSessionFactoryTest2" class="com.yeepay.g3.utils.persistence.mybatis.SqlSessionFactoryBeanWapper"> <property name="dataSource" ref="test2DS" /> <property name="configLocation" value="classpath:/mybatis/mybatis-config.xml" /> <property name="mapperLocations" value="classpath*:test2-core-mapper/*.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.yunpeng.core.test2.dao" /> <property name="annotationClass" value="org.springframework.stereotype.Repository" /> <property name="sqlSessionFactory" ref="sqlSessionFactoryTest2" /> </bean> </beans>
作者:你的雷哥
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。