#35 springboot: external read - @ConfigurationProperties
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
package hello;
|
||||
|
||||
import hello.config.MyDataSourceEnvConfig;
|
||||
import hello.config.MyDataSourceValueConfig;
|
||||
import hello.config.MyDataSourceConfigV1;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
//@Import(MyDataSourceEnvConfig.class)
|
||||
@Import(MyDataSourceValueConfig.class)
|
||||
//@Import(MyDataSourceValueConfig.class)
|
||||
@Import(MyDataSourceConfigV1.class)
|
||||
@SpringBootApplication(scanBasePackages = "hello.datasource")
|
||||
@ConfigurationPropertiesScan
|
||||
public class ExternalReadApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package hello.config;
|
||||
|
||||
import hello.datasource.MyDataSource;
|
||||
import hello.datasource.MyDatasourcePropertiesV1;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
//@EnableConfigurationProperties(MyDatasourcePropertiesV1.class)
|
||||
public class MyDataSourceConfigV1 {
|
||||
|
||||
private final MyDatasourcePropertiesV1 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,26 @@
|
||||
package hello.datasource;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@ConfigurationProperties("my.datasource")
|
||||
public class MyDatasourcePropertiesV1 {
|
||||
|
||||
private String url;
|
||||
private String username;
|
||||
private String password;
|
||||
private Etc etc;
|
||||
|
||||
@Data
|
||||
public static class Etc {
|
||||
|
||||
private int maxConnection;
|
||||
private Duration timeout;
|
||||
private List<String> options = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user