Is it possible to Autowire two different beans of the same class without @qualifier and @resource? If yes, how?

huangapple go评论71阅读模式
英文:

Is it possible to Autowire two different beans of the same class without @qualifier and @resource? If yes, how?

问题

我有一个在JAR文件中的组件,其中有一个自动装配的数据源。

 @Service
 Class ReadService{
      @Autowire
       DataSource readDataSource
 }


@Service
 Class WriteService{
      @Autowire
      DataSource writeDataSource
 }

我必须配置两个不同的服务器,一个用于读取,一个用于写入... 如何将这些数据源注入到从JAR插件创建的这些bean中...

英文:

I have a component in jar fole which has a autowired DataSource

 @Service
 Class ReadService{
      @Autowire
       DataSource readDataSource
 }


@Service
 Class WriteService{
      @Autowire
      DataSource writeDataSource
 }

I have to configure two different severs, one to read and one to write.. how do I inject those DataSource to these beans created from Jar plugin...

答案1

得分: 1

你可以在这里阅读更多信息。

简而言之,你可以在属性文件中指定不同的数据源配置,并分别创建数据源bean。

主要数据源

@Bean
@Primary
@ConfigurationProperties("app.datasource.member")
public DataSourceProperties memberDataSourceProperties() {
    return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties("app.datasource.member.configuration")
public DataSource memberDataSource() {
    return memberDataSourceProperties().initializeDataSourceBuilder()
            .type(HikariDataSource.class).build();
}

次要数据源

/* 持卡人数据源 */
@Bean
@ConfigurationProperties("app.datasource.cardholder")
public DataSourceProperties cardHolderDataSourceProperties() {
    return new DataSourceProperties();
}
@Bean
@ConfigurationProperties("app.datasource.cardholder.configuration")
public DataSource cardholderDataSource() {
    return cardHolderDataSourceProperties().initializeDataSourceBuilder()
            .type(BasicDataSource.class).build();
}
/* 卡片数据源 */
@Bean
@ConfigurationProperties("app.datasource.card")
public DataSourceProperties cardDataSourceProperties() {
    return new DataSourceProperties();
}
@Bean
@ConfigurationProperties("app.datasource.card.configuration")
public DataSource cardDataSource() {
    return cardDataSourceProperties().initializeDataSourceBuilder()
            .type(BasicDataSource.class).build();
}

并在实体声明中指定模式

@Table(name = "member", schema = "memberdb")
英文:

You can read more here

in two words, you can specify different datasource configurations in properties file and create datasource beans separately

app.datasource.member.url=jdbc:mysql://localhost:3306/memberdb?createDatabaseIfNotExist=true
app.datasource.member.username=root
app.datasource.member.password=P@ssw0rd#
app.datasource.member.driverClassName=com.mysql.cj.jdbc.Driver
#card number  (cardholder id, cardnumber)
app.datasource.cardholder.url=jdbc:mysql://localhost:3306/cardholderdb?createDatabaseIfNotExist=true
app.datasource.cardholder.username=root
app.datasource.cardholder.password=P@ssw0rd#
app.datasource.cardholder.driverClassName=com.mysql.cj.jdbc.Driver
#expiration date (card id, expiration month, expiration year)
app.datasource.card.url=jdbc:mysql://localhost:3306/carddb?createDatabaseIfNotExist=true
app.datasource.card.username=root
app.datasource.card.password=P@ssw0rd#
app.datasource.card.driverClassName=com.mysql.cj.jdbc.Driver

For primary Datasource

@Bean
@Primary
@ConfigurationProperties("app.datasource.member")
public DataSourceProperties memberDataSourceProperties() {
    return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties("app.datasource.member.configuration")
public DataSource memberDataSource() {
    return memberDataSourceProperties().initializeDataSourceBuilder()
            .type(HikariDataSource.class).build();

For secondary datasource

/*cardholder data source */
@Bean
@ConfigurationProperties("app.datasource.cardholder")
public DataSourceProperties cardHolderDataSourceProperties() {
    return new DataSourceProperties();
}
@Bean
@ConfigurationProperties("app.datasource.cardholder.configuration")
public DataSource cardholderDataSource() {
    return cardHolderDataSourceProperties().initializeDataSourceBuilder()
            .type(BasicDataSource.class).build();
}
/*card data source*/
@Bean
@ConfigurationProperties("app.datasource.card")
public DataSourceProperties cardDataSourceProperties() {
    return new DataSourceProperties();
}
@Bean
@ConfigurationProperties("app.datasource.card.configuration")
public DataSource cardDataSource() {
    return cardDataSourceProperties().initializeDataSourceBuilder()
            .type(BasicDataSource.class).build();
}

And specify schema in entity declaration

@Table(name = "member", schema = "memberdb")

答案2

得分: 0

如果您基于相同组件构建了2个独立的应用程序,您可以在两个应用程序的application.yml(或application.properties)中简单地使用不同的数据库连接设置。

对于“写入”应用程序:

spring:
  datasource:
    url: jdbc:postgresql://localhost:5433/writeDB
    username: "username"
    password: "password"

对于“读取”应用程序:

spring:
  datasource:
    url: jdbc:postgresql://localhost:5433/readDB
    username: "username"
    password: "password"
英文:

If you build 2 separate applications based on same component, you can simply use different database connection settings in application.yml (or application.properties) of both applications

For "Write" app:

spring:
  datasource:
    url: jdbc:postgresql://localhost:5433/writeDB
    username: "username"
    password: "password"

For "Read" app:

spring:
  datasource:
    url: jdbc:postgresql://localhost:5433/readDB
    username: "username"
    password: "password"

huangapple
  • 本文由 发表于 2020年8月18日 15:26:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63463747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定