简体   繁体   中英

how can I set value dynamic in persistence.xml file

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!

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").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM