java - springboot配置两个数据源怎么实现?
黄舟
黄舟 2017-04-18 09:59:57
0
1
862

springboot如何配置两个数据源呢?我的思路是这样的:
1.application.properties配置文件中先定义好两套数据源,然后定义一个DataSourceConfig类去加载两套数据源,然后再dao层调用mapper来查询的时候,分别用注解配套上不同的数据源,但是代码一直出错,请教有没有这方面的代码可以提供给参考下的,谢谢~~~

我试了下,报这个错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined in URL [jar:file:/E:/wacai/maven_repository/io/springfox/springfox-spring-web/2.1.2/springfox-spring-web-2.1.2.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor argument with index 1 of type [springfox.documentation.spi.service.RequestHandlerProvider]: : Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/E:/wacai/maven_repository/io/springfox/springfox-spring-web/2.1.2/springfox-spring-web-2.1.2.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.util.List]: : Error creating bean with name 'endpointHandlerMapping' defined in class path resource [org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping]: Factory method 'endpointHandlerMapping' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcEndpoints' defined in class path resource [org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'environmentMvcEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.boot.actuate.endpoint.EnvironmentEndpoint]: : Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration.healthIndicators; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration.dataSources; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'donghuaDataSource' defined in file [E:wacaiworkspaceData-Migrationtargetclassesspringdatabase.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to decrypt.; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration.healthIndicators; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration.dataSources; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'donghuaDataSource' defined in file [E:wacaiworkspaceData-Migrationtargetclassesspringdatabase.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to decrypt.; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'endpointHandlerMapping' defined in class path resource [org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping]: Factory method 'endpointHandlerMapping' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcEndpoints' defined in class path resource [org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'environmentMvcEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.boot.actuate.endpoint.EnvironmentEndpoint]: : Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration.healthIndicators; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration.dataSources; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'donghuaDataSource' defined in file [E:wacaiworkspaceData-Migrationtargetclassesspringdatabase.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to decrypt.; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration.healthIndicators; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration.dataSources; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'donghuaDataSource' defined in file [E:wacaiworkspaceData-Migrationtargetclassesspringdatabase.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to decrypt.; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/E:/wacai/maven_repository/io/springfox/springfox-spring-web/2.1.2/springfox-spring-web-2.1.2.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.util.List]: : Error creating bean with name 'endpointHandlerMapping' defined in class path resource [org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping]: Factory method 'endpointHandlerMapping' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcEndpoints' defined in class path resource [org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'environmentMvcEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.boot.actuate.endpoint.EnvironmentEndpoint]: : Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration.healthIndicators; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration.dataSources; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'donghuaDataSource' defined in file [E:wacaiworkspaceData-Migrationtargetclassesspringdatabase.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to decrypt.; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration.healthIndicators; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration.dataSources; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'donghuaDataSource' defined in file [E:wacaiworkspaceData-Migrationtargetclassesspringdatabase.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to decrypt.; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'endpointHandlerMapping' defined in class path resource [org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping]: Factory method 'endpointHandlerMapping' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcEndpoints' defined in class path resource [org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'environmentMvcEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.boot.actuate.endpoint.EnvironmentEndpoint]: : Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration.healthIndicators; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration.dataSources; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'donghuaDataSource' defined in file [E:wacaiworkspaceData-Migrationtargetclassesspringdatabase.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to decrypt.; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration.healthIndicators; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DataSourcesHealthIndicatorConfiguration.dataSources; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'donghuaDataSource' defined in file [E:wacaiworkspaceData-Migrationtargetclassesspringdatabase.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to decrypt.

https://yq.aliyun.com/article...
springboot数据源 注解
springboot官网文档

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

répondre à tous(1)
黄舟

a. Configurez directement 2 ensembles de sources de données comme suit FACILE

@Configuration  
public class SpringConfig {  
  
    @Bean  
    public DataSource dataSource(){  
        return new XXDataSource();  
    } 
    
    @Bean
    public Counter dataSource2(){  
        return new XXDataSource();
    }  
}

b. Complexes d'implémentation de la bibliothèque de séparation en lecture seule de source de données composite

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"
      p:url="${jdbc.url}"
      p:username="${jdbc.username}"
      p:password="${jdbc.password}"
      p:initialSize="${druid.initialSize}"
      p:minIdle="${druid.minIdle}"
      p:maxActive="${druid.maxActive}"
      p:maxWait="${druid.maxWait}"
      p:timeBetweenEvictionRunsMillis="${druid.timeBetweenEvictionRunsMillis}"
      p:minEvictableIdleTimeMillis="${druid.minEvictableIdleTimeMillis}"
      p:validationQuery="${druid.validationQuery}"
      p:poolPreparedStatements="${druid.poolPreparedStatements}"
      p:maxPoolPreparedStatementPerConnectionSize="${druid.maxPoolPreparedStatementPerConnectionSize}"/>

<bean id="slaveDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"
      p:url="${slave.jdbc.url}"
      p:username="${slave.jdbc.username}"
      p:password="${slave.jdbc.password}"
      p:initialSize="${slave.druid.initialSize}"
      p:minIdle="${slave.druid.minIdle}"
      p:maxActive="${slave.druid.maxActive}"
      p:maxWait="${slave.druid.maxWait}"
      p:timeBetweenEvictionRunsMillis="${slave.druid.timeBetweenEvictionRunsMillis}"
      p:minEvictableIdleTimeMillis="${slave.druid.minEvictableIdleTimeMillis}"
      p:validationQuery="${slave.druid.validationQuery}"
      p:poolPreparedStatements="${slave.druid.poolPreparedStatements}"
      p:maxPoolPreparedStatementPerConnectionSize="${slave.druid.maxPoolPreparedStatementPerConnectionSize}"/>

<bean id="complexDataSource" class="com.weghst.pine.complexds.ComplexDataSource">
    <constructor-arg index="0" ref="dataSource"/>
    <constructor-arg index="1">
        <map>
            <entry key="#{T(com.weghst.pine.complexds.NamedDS).DEFAULT_SLAVE_NAME}" value-ref="slaveDataSource"/>
        </map>
    </constructor-arg>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
      p:dataSource-ref="complexDataSource"/>

<tx:annotation-driven transaction-manager="transactionManager"/>

<!--
    MyBatis 配置
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="complexDataSource"/>
    <property name="configurationProperties">
        <props>
            <prop key="defaultScriptingLanguage">org.mybatis.scripting.freemarker.FreeMarkerLanguageDriver</prop>
        </props>
    </property>
    <property name="mapperLocations" value="classpath*:com/weghst/pine/repository/*Mapper.xml"/>
</bean>
<bean id="sqlSessionTemplate" class="com.weghst.pine.util.FastSqlSessionTemplate"
      c:sqlSessionFactory-ref="sqlSessionFactory"/>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.weghst.pine.repository"/>
    <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
    <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"/>
</bean>

<bean id="complexDataSourceInterceptor" class="com.weghst.pine.complexds.ComplexDataSourceInterceptor"/>
<aop:config>
    <aop:aspect ref="complexDataSourceInterceptor">
        <aop:around method="invoke" pointcut="execution(* com.weghst.pine.repository.*.*(..))"/>
    </aop:aspect>
</aop:config>

En fait, le principe de mise en œuvre est de configurer plusieurs sources de données différentes et de les concentrer dans ComplexDS pour les distinguer par leur nom. Utilisez Repository pour préciser la source de données dans la méthode @NamedDS, et quand MyBatis. obtient Connection Utilisez AOP pour renvoyer automatiquement différents Connection en fonction de différentes sources de données.
ComplexDataSource : ComplexDataSource.java
Spring AOP : ComplexDataSourceInterceptor.java

Utilisation :

@NamedDS
List<Config> findAll();
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal