我的应用程序具有applicationContext.xml,其中EntityManagerFactory bean定义为:

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="org.xyz" />
**<property name="dataSource" ref="poolDVLDataSource" />**
<!--<property name="dataSource" ref="poolPRDDataSource" /> -->
<property name="jpaVendorAdapter">
  <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="databasePlatform"
            value="org.hibernate.dialect.Oracle10gDialect" />
    <property name="database" value="ORACLE" />
    <property name="showSql" value="false" />
  </bean>
</property>
</bean>

和数据源引用为
 <bean id="poolPRDDataSource" class="org.apache.commons.dbcp.BasicDataSource">
 <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
 ....
</bean>


<bean id="poolDVLDataSource" class="org.apache.commons.dbcp.BasicDataSource">
   <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
  ....
 </bean>

我正在使用gradle构建。根据部署环境,是否有办法动态地将dataSource ref替换为“poolDVLDataSource”或“poolPRDDataSource”?

最佳答案

ReplaceRegExp ant任务应该可以解决您的问题。 https://ant.apache.org/manual/Tasks/replaceregexp.html

以下示例代码示例:

ant.replaceregexp(match:'existingName', replace:'newName', byline:true) {
    fileset(dir: 'WebContent/WEB-INF', includes: 'applicationContext.xml')
}

08-27 23:09