0

I have created a standalone boot.jar that I need to start integrating into our higher environments. Each environment has a property file that contains database specific connection information. Since this does not live in my boot jar, I would like to somehow add the path to this database.properties file and then read the entries based on key. Used to create a bean like so:

<bean id="propertyLoader" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
        <value>classpath:application.properties</value>
</property>

but with boot, I am not sure how to do this: But I want to point to this below property example and pull out my values and somehow populate the application.properties values that I hardcoded in dev.

server:/config/database.properties
jdbc.username=TEST
jdbc.password=CHANGEME

Updating my application.properites:

spring.datasource.username='jdbc.username'
spring.datasource.password='jdbc.password'

Something like that do I can parameterize my application.properties file.

CardsFan
  • 75
  • 1
  • 11

2 Answers2

1

you already told you can not have property files inside your jar, still there are multiple options.

1> passing a property file for respective env.

java -jar myproject.jar --spring.config.location=classpath:/database.properties 

2> pass properties while calling the jar

java -jar app.jar --spring.datasource.username="jdbc.username" --spring.datasource.password="jdbc.password"

Read a lot of other options here `

I would go with option 1, because passing credentials is never advisable in arguements.

surya
  • 2,581
  • 3
  • 22
  • 34
1

SpringBoot offers profiles, which basically allows you to have separate application.properties file for each environment.

You can have something like this:

public interface DataSourceConfig {}

@Component
@Profile("dev")
public DevDataSourceConfig implements DataSourceConfig{}

@Component
@Profile("prod")
public ProdDataSourceConfig implements DataSourceConfig{}

If you have the spring profile "dev" set as active, only the DevDataSourceConfig bean will be instantiated and in Spring Environment the properties that will be injected, will be read from the application-dev.properties file.

Similarly when you have the "prod" profile activated, only the ProdDataSourceConfig will be instantiated and the properties will be loaded from application-prod.properties file.

This allows you to have:

---
application-dev.properties
spring.datasource.username='jdbc.username'
spring.datasource.password='jdbc.password'
---
application-prod.properties
spring.datasource.username='PROD_jdbc.username'
spring.datasource.password='PROD_jdbc.password'

If you want to load the configuration from a custom location on the file system - you can check how to pass the location with command line arguments (docs)

Example:

java -jar boot.jar --spring.config.location=classpath:/database.properties
hovanessyan
  • 30,580
  • 6
  • 55
  • 83