繁体   English   中英

Spring 引导多数据源设置

[英]Spring Boot Multiple Datasource Set up

我是springboot的新手,我需要在我的项目中设置多个数据库我正在使用postgresql这是我的属性文件。

如果我正在运行我的应用程序,那么无论@primay 注释我只提供我能够访问的数据库,但我需要访问两个数据库。

注意:如果我同时添加了 db @primary 注释,那么会出现 postconstructor 0 错误。 我在 if else 块中的查询有时我需要访问 abc db 有时我需要访问 xyz db 但是在 springconfig 中我只保留@primary 我得到的那个 db url

我尝试让@primary 都获得异常,我尝试@configurationProperties 在我的springconfig 文件中添加获得异常

spring.datasource1.url=jdbc:postgresql://100.17.13.26:123/abc
spring.datasource1.username=ENC(vAIVaqTTZ89eYBWBDbUxgGdhciXm3GuB)
spring.datasource1.password=ENC(abZypzjEuvLfbovYs0oGdeRnUqM8e+k1)
spring.datasource1.driver-class-name=org.postgresql.Driver

spring.datasource2.url=jdbc:postgresql://100.17.13.26:123/xyz
spring.datasource2.username=ENC(vAIVaqTTZ89eYBWBDbUxgGdhciXm3GuB)
spring.datasource2.password=ENC(abZypzjEuvLfbovYs0oGdeRnUqM8e+k1)
spring.datasource2.driver-class-name=org.postgresql.Driver

我添加了@springBootApplication SpringConfig class

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@Configuration
public class SpringConfiguration {
    @Autowired
    private Environment env;

    
    @Bean(name = "abc")
    public DataSource firstDataSource() {
        System.out.println("from first DB");
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("spring.datasource1.driver-class-name"));
        dataSource.setUrl(env.getProperty("spring.datasource1.url"));
        dataSource.setUsername(env.getProperty("spring.datasource1.username"));
        dataSource.setPassword(env.getProperty("spring.datasource1.password"));
        System.out.println("from first DB end");
        return dataSource;
    }
    @Primary
    @Bean(name = "xyz")
    public DataSource secondDataSource() {
        System.out.println("from second DB");
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("spring.datasource2.driver-class-name"));
        dataSource.setUrl(env.getProperty("spring.datasource2.url"));
        dataSource.setUsername(env.getProperty("spring.datasource2.username"));
        dataSource.setPassword(env.getProperty("spring.datasource2.password"));
        System.out.println("from second DB end");
        return dataSource;
    }

    @Bean(name = "abc")
    public JdbcTemplate template(@Qualifier("abc") DataSource ds) {
        System.out.println("calling first db");
        return new JdbcTemplate(ds);
    }

    @Bean(name = "xyz")
    public JdbcTemplate template1(@Qualifier("xyz") DataSource ds) {
        System.out.println("calling second db");
        return new JdbcTemplate(ds);
    }

}

在访问第二个非主数据库时,您可能必须使用 @Qualifier。

@Autowired
@Qualifier("abc") 
DataSource dataSource;

JdbcTemplate bean不应与 DataSource bean 具有相同的名称。

这个简单的例子不能启动:

// In Config.java
@Configuration
class Config{
  @Bean("a")
  public String a() {
    return "a";
  }

  @Bean("b")
  public String b() {
    return "b";
  }

  @Bean("a")
  public Integer c(@Qualifier("a") String a) {
    return a.length();
  }

  @Bean("b")
  public Integer d(@Qualifier("b") String b) {
    return b.length();
  }
}

// In Comp.java
@Component
public static class Comp {
  private final String s;
  private final Integer i;

  public Comp(@Qualifier("a") String s, @Qualifier("b") Integer i) {

    this.s = s;
    this.i = i;
  }

  @EventListener(ApplicationReadyEvent.class)
  public void ok() {
    System.out.println(s);
    System.out.println(i);
  }
}

因为你的配置和上面的一样,它也不会启动!

如何解决:

在上面的示例中,如果将Integer bean 上的名称“a”和“b”更改为“c”和“d”,并相应地更改Comp构造函数中的限定符值,则可以正常工作。

ps 我不建议您使用Environment bean 来获取您的配置值; 我个人使用@Value("${the.key}")代替。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM