A commonly used strategy is to define all run-time configurations in one or more * .properties files and use the spring PropertyPlaceholderConfigurer to load values ββand replace the replacement in applicationContext.xml, in more detail here: Best ways to handle property values ββin an XML file in Spring, Maven and eclipes .
app.properties:
ApplicationContext-dataStore.xml:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:app.properties</value> <value>file:///opt/my-app/conf/app.properties</value> </list> </property> <property name="ignoreResourceNotFound" value="true"/> </bean> ... ... <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${hibernate.connection.driver_class}" /> <property name="url" value="${hibernate.connection.url}" /> <property name="username" value="${hibernate.connection.username}" /> <property name="password" value="${hibernate.connection.password}" /> </bean>
One of the problems is that PropertyPlaceholderConfigurer does not analyze persistence.xml, the solution is to move the entire hibernate configuration to spring applicationContext.xml, since there is no need to set them in the persistence.xml file. Read more here: loading .properties in spring -context.xml and persistence.xml .
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_1_0.xsd" version="1.0"> <persistence-unit name="JPAService" transaction-type="RESOURCE_LOCAL"/> </persistence>
ApplicationContext-datSource.xml:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${hibernate.connection.driver_class}"/> <property name="url" value="${hibernate.connection.url}"/> <property name="username" value="${hibernate.connection.username}"/> <property name="password" value="${hibernate.connection.password}"/> </bean> ... ... <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceXmlLocation" value="classpath:./META-INF/persistence.xml"/> <property name="persistenceUnitName" value="JPAService"/> <property name="dataSource" ref="dataSource"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="databasePlatform" value="${hibernate.dialect}"/> <property name="showSql" value="true" /> <property name="generateDdl" value="true"/> </bean> </property> <property name="jpaProperties"> <props> <prop key="hibernate.hbm2ddl.auto">${hbm2ddl.auto}</prop> </props> </property> </bean>
Please note that the web application needs to be restarted every time you change the configuration in / opt / my -app / conf / app.properties for the changes to take effect.
Hope this helps.
yorkw source share