Persistence.xml field values ​​from properties file

Help, I would like my xml property constant to reference my db.properties file.

here is my db.properties file

jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/apsas jdbc.username=root jdbc.password=password 

and here is my current persistence.xml

 <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="apsaspu" transaction-type="RESOURCE_LOCAL"> <provider> org.hibernate.ejb.HibernatePersistence </provider> <properties> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/apsas" /> <property name="hibernate.connection.username" value="root" /> <property name="hibernate.connection.password" value="password" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit> </persistence> 

what I wanted to do was set its property like this:

 <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="apsaspu" transaction-type="RESOURCE_LOCAL"> <provider> org.hibernate.ejb.HibernatePersistence </provider> <properties> <property name="hibernate.connection.driver_class" value="${jdbc.driver}" /> <property name="hibernate.connection.url" value="${jdbc.url}" /> <property name="hibernate.connection.username" value="${jdbc.username}" /> <property name="hibernate.connection.password" value="${jdbc.password}" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit> </persistence> 

The question is how can I import the properties file into my x persistence xml Thank you for those who help me ...

+6
source share
1 answer

If you use maven, you can use source filtering for this. Use this document for reference. I will outline the process a bit.

You will need to specify a filter path. I'm not sure if you need to explicitly define the included files.

 <build> ... <resources> <resource> <directory>src/main/resources/META-INF</directory> <filtering>true</filtering> <includes> <include>**/*.xml</include> </includes> </resource> ... </resources> ... </build> 

The directory you define is related to pom.xml .

Now you can define regular maven properties to replace placeholders in persistence.xml

If you want to have properties in a separate .properties file, you need to tell maven where to find this file:

 <filters> <filter>db.properties</filter> </filters> 

Filtering occurs on mvn resources:resources . This step is defined for all packaging purposes that you will perform during deployment.

You can use maven profiles to switch between sets of properties or .properties files.

+3
source

All Articles