I am currently having trouble with my dataSource bean creation on condition of String property from my applications.yaml file.
Ideally, I would only like to create the dataSource bean only if the url is set in my application.yaml file. Shouldn't create the bean if its not present (empty or null). I know this condition checks on boolean but is there anyway to check if the string property is empty or null?
DatabaseConfig.java
@Configuration
public class DatabaseConfig {
@Value("${database.url:}")
private String databaseUrl;
@Value("${database.username:}")
private String databaseUsername;
@Value("${database.password:}")
private String databasePassword;
protected static final String DRIVER_CLASS_NAME = "com.sybase.jdbc4.jdbc.SybDriver";
/**
* Configures and returns a datasource. Optional
*
* @return A datasource.
*/
@ConditionalOnProperty(value = "database.url")
@Bean
public DataSource dataSource() {
return DataSourceBuilder.create()
.url(testDatabaseUrl)
.username(testDatabaseUsername)
.password(testDatabasePassword)
.build();
}
}
application.yml (This field will be optional)
database:
url: http://localhost:9000