When using JdbcTemplates in SpringBoot app, generally we set below two fields in application.properties like
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=admin
Then @Autowire JdbcTemplate in our application and run our query. Is it allowed to configure spring.datasource.password=admin using java code instead of application.properties. I don't want to put my password in application.properties.
Maybe configure DataSource bean and set all values in it.
I hope it will suit you
@Configuration
public class DataSourceConfig {
@Bean
public DataSource getDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("org.h2.Driver");
dataSourceBuilder.url("jdbc:h2:mem:test");
dataSourceBuilder.username("SA");
dataSourceBuilder.password("");
return dataSourceBuilder.build();
}
}
Remove this info from properties and write here. Be aware that hardcoded password is also unsecured, the code might be decompiled. It is better to read password from environment
dataSourceBuilder.password(System.getEnv("MY_PASSWORD")
This example is borrowed from https://www.baeldung.com/spring-boot-configure-data-source-programmatic