Here is the application.properties
of my Spring Boot 2.5 :
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://${DB_HOST:localhost}:${DB_PORT:3306}/${DB_NAME}
spring.datasource.username=${MYSQL_USER:root}
spring.datasource.password=${MYSQL_PASSWORD:root}
I migrated my application from Spring MVC 4.3 and I don't use JPA on the latest version of Spring Boot.
So I configured a @Bean
datasource like this:
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getRequiredProperty("spring.datasource.driver-class-name"));
dataSource.setUrl(env.getRequiredProperty("spring.datasource.url"));
dataSource.setUsername(env.getRequiredProperty("spring.datasource.username"));
dataSource.setPassword(env.getRequiredProperty("spring.datasource.password"));
return dataSource;
}
However, this does not work because instead of having the value of the environment variable, I have the name of the environment variable, i.e. ${MYSQL_USER:root}
for example.
My question is what is Spring's recommended way here to set up environment variables much like Laravel's .env in my datasource() and also in the application.properties if I decide to use JPA later on?
The reason and the important point: I don't want to push my credentials on git
EDIT :
HibernateUtil :
static {
try {
Properties applicationProps = new Properties();
File file = ResourceUtils.getFile("classpath:application.properties");
InputStream input = new FileInputStream(file);
applicationProps.load(input);
Properties properties = new ApplicationConf().hibernateProperties();
// Configure datasource
properties.setProperty("hibernate.connection.driver_class", applicationProps.getProperty("spring.datasource.driver-class-name"));
properties.setProperty("hibernate.connection.url", applicationProps.getProperty("spring.datasource.url"));
properties.setProperty("hibernate.connection.username", applicationProps.getProperty("spring.datasource.username"));
properties.setProperty("hibernate.connection.password", applicationProps.getProperty("spring.datasource.password"));
properties.setProperty("hibernate.current_session_context_class", "thread");
properties.setProperty("hibernate.jdbc.batch_size", Integer.toString(BATCH_SIZE));
// Override some properties
properties.setProperty("hibernate.format_sql", "false");
properties.setProperty("hibernate.show_sql", "false");
} catch {}
...
}
Best regards,