#35 springboot: external read - @ConfigurationProperties with constructor
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package hello;
|
||||
|
||||
import hello.config.MyDataSourceConfigV1;
|
||||
import hello.config.MyDataSourceConfigV2;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
|
||||
@@ -8,7 +9,8 @@ import org.springframework.context.annotation.Import;
|
||||
|
||||
//@Import(MyDataSourceEnvConfig.class)
|
||||
//@Import(MyDataSourceValueConfig.class)
|
||||
@Import(MyDataSourceConfigV1.class)
|
||||
//@Import(MyDataSourceConfigV1.class)
|
||||
@Import(MyDataSourceConfigV2.class)
|
||||
@SpringBootApplication(scanBasePackages = "hello.datasource")
|
||||
@ConfigurationPropertiesScan
|
||||
public class ExternalReadApplication {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package hello.config;
|
||||
|
||||
import hello.datasource.MyDataSource;
|
||||
import hello.datasource.MyDatasourcePropertiesV2;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@EnableConfigurationProperties(MyDatasourcePropertiesV2.class)
|
||||
public class MyDataSourceConfigV2 {
|
||||
|
||||
private final MyDatasourcePropertiesV2 properties;
|
||||
|
||||
@Bean
|
||||
public MyDataSource dataSource() {
|
||||
return new MyDataSource(
|
||||
properties.getUrl(),
|
||||
properties.getUsername(),
|
||||
properties.getPassword(),
|
||||
properties.getEtc().getMaxConnection(),
|
||||
properties.getEtc().getTimeout(),
|
||||
properties.getEtc().getOptions()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package hello.datasource;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.bind.DefaultValue;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@ConfigurationProperties("my.datasource")
|
||||
public class MyDatasourcePropertiesV2 {
|
||||
|
||||
private String url;
|
||||
private String username;
|
||||
private String password;
|
||||
private Etc etc;
|
||||
|
||||
public MyDatasourcePropertiesV2(String url, String username, String password, @DefaultValue Etc etc) {
|
||||
this.url = url;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.etc = etc;
|
||||
}
|
||||
|
||||
@Getter
|
||||
public static class Etc {
|
||||
|
||||
private int maxConnection;
|
||||
private Duration timeout;
|
||||
private List<String> options;
|
||||
|
||||
public Etc(int maxConnection, Duration timeout, @DefaultValue("DEFAULT") List<String> options) {
|
||||
this.maxConnection = maxConnection;
|
||||
this.timeout = timeout;
|
||||
this.options = options;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
my.datasource.url=local.db.com
|
||||
my.datasource.username=username
|
||||
my.datasource.password=password
|
||||
my.datasource.etc.max-connection=1
|
||||
my.datasource.etc.timeout=3500ms
|
||||
my.datasource.etc.options=CACHE,ADMIN
|
||||
#my.datasource.etc.max-connection=1
|
||||
#my.datasource.etc.timeout=3500ms
|
||||
#my.datasource.etc.options=CACHE,ADMIN
|
||||
|
||||
Reference in New Issue
Block a user