有没有一种方法可以在spring-context.xml和JPA persistence.xml中引用.properties文件?
我想我在春天的上下文文件中看到了一个这样的例子,尽管我不记得在哪里。也许有人知道吗?关于persistence.xml,我实际上不确定这是否奏效。
我的目标是在开发和发行配置之间更改某些属性。我目前的想法是通过模板配置中的ant手动替换文件中的所有属性。尽管应该有更好的方法来执行此操作。
你可以使用PropertyPlaceholderConfigurer从Spring bean定义文件中引用外部属性文件。我认为这不适用于JPA persistence.xml,尽管Spring的JPA支持允许你将persistence.xml的大部分(即使不是全部)内容合并到bean文件本身中,在这种情况下它将很好地工作。
无需使用构建来创建persistence.xml的产品或开发版本,只需将所有属性设置移至spring内容即可。
我的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-unit> </persistence>
在我的spring内容中,我然后使用PropertyPlaceholderConfigurer读取dev / prod属性值并将其设置到entityManagerFactory bean中
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/> <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> <bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> <property name="ignoreResourceNotFound" value="true"/> <property name="locations"> <list> <value>classpath:dev.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${datasource.driverClassName}"/> <property name="url" value="${datasource.url}"/> <property name="username" value="${datasource.username}"/> <property name="password" value="${datasource.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="org.hibernate.dialect.OracleDialect"/> <property name="showSql" value="true" /> <property name="generateDdl" value="true"/> </bean> </property> <property name="jpaProperties"> <!-- set extra properties here, e.g. for Hibernate: --> <props> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/> </beans>