@MyAutoConfiguration
public class DataSourceConfig {
}
implementation이 아닌 runtimeOnly인 이유?
코드에서 직접 드라이버 클래스를 사용할 일이 없기 때문에
@ConditionalMyOnClass
추가@Bean
추가 - SimpleDriverDataSource
사용MyDataSourceProperties
클래스 작성 후 사용@EnableMyConfigurationProperties(MyDataSourceProperties)
MyDataSourceProperties
@MyConfigurationProperties(prefix = "data")
public class MyDataSourceProperties {
private String driverClassName;
private String url;
private String username;
private String password;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
DataSourceConfig
@MyAutoConfiguration
@ConditionalMyOnClass("org.springframework.jdbc.core.JdbcOperations")
@EnableMyConfigurationProperties(MyDataSourceProperties.class)
public class DataSourceConfig {
@Bean
DataSource dataSource(MyDataSourceProperties properties) throws ClassNotFoundException {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriverClass((Class<? extends Driver>) Class.forName(properties.getDriverClassName()));
dataSource.setUrl(properties.getUrl());
dataSource.setUsername(properties.getUsername());
dataSource.setPassword(properties.getPassword());
return dataSource;
}
}
잘 시작 됐는지 서버를 시작해서 확인