0

I'm working in on spring boot project ( with maven) I have a file application.properties for config like this:

jdbc.username=postgres
jdbc.password=40love

In persistence.xml file instead of using the username and password like this :

<properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL81Dialect" />
    <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
    <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres" />
    <property name="javax.persistence.jdbc.user" value="${env.jdbc.username}" />
    <property name="javax.persistence.jdbc.password" value="${env.jdbc.password}" />
</properties>

But it cannot replace username in persistence.xml. Is it posible replace that? Thanks!

1 Answers1

0

Yes there are indeed a lot of ways to replace values, this is called Externalized Configuration

For example you could use commandline parameters when starting the application:

java -jar main.jar --jdbc.username=postgres --jdbcpassword=40love

On your application classpath (for example, inside your jar) you can have an application.properties file that provides a sensible default property value for name. When running in a new environment, an application.properties file can be provided outside of your jar that overrides the name. For one-off testing, you can launch with a specific command line switch (for example, java -jar app.jar --name="Spring").

Tobias Otto
  • 1,634
  • 13
  • 20