我有一个Spring Integration WAR组件,我正在更新以在私有PCF中运行。我在应用程序中定义了两个DataSource和一个RabbitMQ连接工厂。
我看到Thomas Risberg撰写的关于使用云命名空间和同时处理多个服务的文章 - https://spring.io/blog/2011/11/09/using-cloud-foundry-services-with-spring-part-3-the-cloud-namespace。这是通过使用@Autowired和@Qualifier注释来处理的。
我想知道如果我们不是@Autowired和@Qualifier注释,例如,如何实现这一点,例如将DataSource连接到JdbcTemplate。在这里,我们无法指定@Qualifier注释。
我的应用程序是基于Spring XML配置的。我有能力在其中一个DataSource上使用@Autowired和@Qualifier注释,但另一个是JPA实体管理器。请参阅代码段。
非常感谢任何帮助。
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="activity-monitor" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jpaProperties">
<value>
hibernate.format_sql=true
</value>
</property>
</bean>
<beans profile="cloud">
<cloud:data-source id="dataSource" service-name="actmon-db-service" />
</beans>
Java Build Pack:java_buildpack_offline java-buildpack-offline-v2.4.zip Spring自动重新配置版本1.4.0。
更新:这是两个数据源的完整配置,包括使用DAO从数据源加载属性的PropertySourcesPlaceholderConfigurer。
<bean id="cic.application.ppc" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="properties" ref="cic.application.properties"/>
<property name="locations" ref="cic.application.propertyLocations"/>
</bean>
<bean id="cic.application.properties" class="java.util.Properties">
<constructor-arg value="#{cicPropertiesService.properties}"></constructor-arg>
</bean>
<bean id="cic.properties.propertiesService" name="cicPropertiesService"
class="com.emc.it.eis.properties.service.DefaultPropertiesService">
<constructor-arg index="0"
ref="cic.properties.propertiesDao" />
</bean>
<bean id="cic.properties.propertiesDao" class="com.emc.it.eis.properties.dao.JdbcPropertiesDao">
<constructor-arg ref="cic.properties.dataSource" />
</bean>
<beans profile="default">
<jee:jndi-lookup id="cic.properties.dataSource"
jndi-name="jdbc/intdb" />
</beans>
<beans profile="cloud">
<cloud:data-source id="cic.properties.dataSource" service-name="oracle-cicadm-db-service" />
</beans>
<beans>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="actmonDataSource" />
<property name="persistenceUnitName" value="activity-monitor" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jpaProperties">
<value>
hibernate.format_sql=true
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
<beans profile="default">
<jee:jndi-lookup id="dataSource"
jndi-name="jdbc/actmon" />
</beans>
<beans profile="cloud">
<cloud:data-source id="actmonDataSource" service-name="postgres-actmon-db-service" />
</beans>
<beans profile="default,cloud">
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="POSTGRESQL" />
</bean>
</beans>
部署https://gist.github.com/anonymous/3986a1a7cea4f20c096e时来自CF的输出。请注意,它正在跳过javax.sql.DataSources
的自动重新配置答案 0 :(得分:0)
首先,托马斯的帖子很老了,引用了一个已弃用的支持库。您应该使用Spring Cloud Connectors dependencies而不是org.cloudfoundry:cloudfoundry-runtime:0.8.1
依赖项。
然后,您可以按照instructions provided使用Spring Cloud Connectors进行XML配置。对于同一类型的多个服务,您需要为每个bean指定服务的名称。按照您的示例,假设您创建了两个名为inventory-db
和customer-db
的CF数据库服务,它们可能如下所示:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="inventory-dataSource" />
<property name="persistenceUnitName" value="activity-monitor" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jpaProperties">
<value>
hibernate.format_sql=true
</value>
</property>
</bean>
<beans profile="cloud">
<cloud:data-source id="inventory-dataSource" service-name="inventory-db">
<cloud:data-source id="customer-dataSource" service-name="customer-db">
</beans>
答案 1 :(得分:0)
我设法通过使用spring cloud使用的工厂bean解决了这个问题:data-source,CloudDataSourceFactory
。创建此实例并连接配置,包括CF服务的service-name
。这避免了我们的PropertySourcesPlaceholderConfigurer在我们的bean被定义之前尝试使用数据源的问题。
<!--
configure cloud data source for using CloudDataSourceFactory; this is what spring cloud:data-source is using;
required to manually wire this data source bean as cloud:data-source bean gets defined in a phase after our
PropertySourcesPlaceholderConfigurer bean.
-->
<bean id="cic.properties.dataSource" class="org.springframework.cloud.service.relational.CloudDataSourceFactory">
<constructor-arg value="oracle-cicadm-db-service" />
<constructor-arg>
<!-- configuring minimal data source as it is used only to bootstrap properties on app start-up -->
<bean class="org.springframework.cloud.service.relational.DataSourceConfig">
<constructor-arg>
<bean class="org.springframework.cloud.service.PooledServiceConnectorConfig.PoolConfig">
<constructor-arg value="0" />
<constructor-arg value="2" />
<constructor-arg value="180" />
</bean>
</constructor-arg>
<!-- ConnectionConfig not required for cic.properties.dataSource so setting to null -->
<constructor-arg value="#{ null }" />
</bean>
</constructor-arg>
</bean>