vaibhav
vaibhav

Reputation: 4103

Configuring JNDI Spring boot application

I am using a Spring boot application where the default configuration of datasouce is done using the following properties in application.properties:

spring.datasource.driverClassName=${datasource.driver.className}
spring.datasource.url=${datasource.url}
spring.datasource.username=${datasource.username}
spring.datasource.password=${datasource.password}

The thing is that this works well when i run the spring boot run command through local maven, however i need to configure the things in such a way that when i try to to a maven build to create a war the configuration puts a JNDI name as:

spring.datasource.jndi-name=java:jboss/datasources/

Can i make it configurable through maven or any other way that when we deploy it through Spring boot it picks above 4 properties and when we create a war it picks only the JNDI property.

-Vaibhav

Upvotes: 1

Views: 2420

Answers (1)

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44755

You can use Spring profiles for that. For example, create a file called application-local.properties and add the first four properties to it:

# application-local.properties
spring.datasource.driverClassName=${datasource.driver.className}
spring.datasource.url=${datasource.url}
spring.datasource.username=${datasource.username}
spring.datasource.password=${datasource.password}

Then create another file names application-jndi.properties:

# application-jndi.properties
spring.datasource.jndi-name=java:jboss/datasources/

Now all you have to do is to add the VM argument -Dspring.profiles.active=local (or add the SPRING_PROFILES_ACTIVE=local environment variable) to the command when you run it locally, and -Dspring.profiles.active=jndi when you need to run it with JNDI.

It isn't exactly automatic depending on the Maven packaging type, but configuring the Spring profile per environment isn't difficult either. You could also set one of the both configuration properties as default (in application.properties) so you don't have to configure the profile as much.

Upvotes: 1

Related Questions