0

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 @Beandatasource 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,

Jedupont
  • 401
  • 1
  • 4
  • 12

1 Answers1

0

You can use the @Value annotation to pull the properties and also their defaults.

@Value("${spring.datasource.username}")
private String userName;

@Bean
public DataSource dataSource() {
...
    dataSource.setUsername(userName);
...
    return dataSource;
}

You can also omit the default from the application.properties file and specify a default value for the property using Value annotation -

@Value("${spring.datasource.username:root}")
private String userName;

@Bean
public DataSource dataSource() {
...
    dataSource.setUsername(userName);
...
    return dataSource;
}

Or can even be injected in the method-

@Bean
public DataSource dataSource(@Value("${spring.datasource.username:root}") String userName) {
...
    dataSource.setUsername(userName);
...
    return dataSource;
}


Yatharth Ranjan
  • 461
  • 2
  • 6
  • The default value can be assigned using ":" in both application.properties and @Value both. In both cases if the environment does not pass the value then the default will be assigned. – Saurabh Singh Jun 04 '21 at 15:01
  • Using default values in `application.properties` and following the `@Value` annotation i got it working! Thanks – Jedupont Jun 04 '21 at 15:11
  • Ok thanks, guys updated the answer to include the changes. @Jedupont great you got it working! Can you please mark the answer as accepted? Thanks! – Yatharth Ranjan Jun 04 '21 at 15:35
  • Yes, but I can't make it work for my "HibernateUtil" which is static and so I can't use the @Value annotation it seems. – Jedupont Jun 04 '21 at 16:04
  • (I added the HibernateUtil on my first post) – Jedupont Jun 04 '21 at 16:25
  • Hi @Jedupont, is there a particular reason this has to be static? As mentioned [here](https://stackoverflow.com/a/7253751/8175739) it is probably best to avoid static fields when using spring. – Yatharth Ranjan Jun 04 '21 at 18:53