当您可以使用以下代码定义多个数据源时,我试图模仿Springs Boot DataSourceBuilder
的行为。
@Bean
@Qualifier("dataflow")
@ConfigurationProperties(prefix = "dataflow")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Qualifier("task")
@ConfigurationProperties(prefix = "task")
public DataSource taskDataSource() {
return DataSourceBuilder.create().build();
}
我已根据问题Binding @ConfigurationProperties to builder used to create bean开发了一些解决方案,它的工作原理如下所述。
我正在尝试基于Foo
前缀创建多个ConfigurationProperties
对象。 Foo
类看起来像这样。
Foo.java
public class Foo {
private String name, surname;
public Foo(String name, String surname) {
this.name = name;
this.surname = surname;
}
@Override
public String toString() {
return "Foo{" +
"name='" + name + '\'' +
", surname='" + surname + '\'' +
'}';
}
}
我还创建了简单的FooBuilder
FooBuilder.java
public class FooBuilder {
private String name, surname;
public static FooBuilder create(){
return new FooBuilder();
}
public FooBuilder name(String name) {
this.name = name;
return this;
}
public FooBuilder surname(String surname) {
this.surname = surname;
return this;
}
public Foo build(){
return new Foo(name,surname);
}
}
然后我创建了具有FooProperites
方法的initializeBuilder
组件。
FooProperties.java
@Component
public class FooProperties {
private String name, surname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public FooBuilder initializeBuilder(){
return FooBuilder.create().name(getName()).surname(getSurname());
}
}
然后,我创建一些InitConfiguration
类,以便以后与InitTest
CommandLineRunner
InitConfiguration.java
@Configuration
public class InitConfiguration {
@Bean
@ConfigurationProperties("t1.foo")
public FooProperties firstFooProperties() {
return new FooProperties();
}
@Bean
public Foo firstFoo(FooProperties firstFooProperties) {
return firstFooProperties.initializeBuilder().build();
}
@Bean
@ConfigurationProperties("t2.foo")
public FooProperties secondFooProperties() {
return new FooProperties();
}
@Bean
public Foo secondFoo(FooProperties secondFooProperties) {
return secondFooProperties.initializeBuilder().build();
}
}
InitTest.java
@Component
public class InitTest implements CommandLineRunner {
private Foo foo1,foo2;
public InitTest(Foo firstFoo, Foo secondFoo) {
this.foo1 = firstFoo;
this.foo2 = secondFoo;
}
@Override
public void run(String... args) throws Exception {
System.out.println(foo1);
System.out.println(foo2);
}
}
基于以下application.properties
文件:
t1.foo.name=a
t1.foo.surname=b
t2.foo.name=x
t2.foo.surname=z
一切正常,并产生以下输出:
Foo{name='a', surname='b'}
Foo{name='x', surname='z'}
问题是如何在不显式定义属性bean的情况下获得相同的结果,以便initConfiguration中的代码如下所示:
@Configuration
public class InitConfiguration {
@Bean
@ConfigurationProperties("t2.foo")
public Foo firstFoo() {
return FooBuilder.create().build();
}
@Bean
@ConfigurationProperties("t2.foo")
public Foo secondFoo() {
return FooBuilder.create().build();
}
}
,输出为:
Foo{name='null', surname='null'}
Foo{name='null', surname='null'}
以这种方式编写时,其作用相同:
@Configuration
public class InitConfiguration {
@Bean
@ConfigurationProperties("t1.foo")
public Foo firstFoo(FooProperties properties) {
return properties.initializeBuilder().build();
}
@Bean
@ConfigurationProperties("t2.foo")
public Foo secondFoo(FooProperties properties) {
return properties.initializeBuilder().build();
}
}
我怀疑它与Spring Boot
的自动配置功能有关,但是我无法从DataSourceBuilder
,DataSourceProperties
,DataSourceConfiguration
的Spring实现中找出来, DataSourceAutoConfiguration
。
问题是...如何实现?
请为我的问题HERE找到我的GitLab存储库。