0

我有一个 Spring Integration WAR 组件,我正在更新它以在私有 PCF 中运行。我在应用程序中定义了两个数据源和一个 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 配置的。我确实有能力在其中一个数据源上使用 @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 Auto-reconfiguration version 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 的自动重新配置

4

2 回答 2

0

首先,Thomas 的帖子已经很老了,并且引用了一个已弃用的支持库。而不是org.cloudfoundry:cloudfoundry-runtime:0.8.1依赖项,您应该使用Spring Cloud Connectors 依赖项。

然后,您可以按照提供的说明将 XML 配置与 Spring Cloud 连接器一起使用。对于相同类型的多个服务,您需要为每个 bean 指定服务的名称。按照您的示例,假设您创建了两个名为inventory-dband的 CF 数据库服务customer-db,可能看起来像这样:

<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>
于 2015-06-16T15:55:58.530 回答
0

我已经设法通过使用 spring cloud:data-source 使用的工厂 bean 解决了这个问题CloudDataSourceFactory。创建此实例并连接配置,包括service-nameCF 服务的配置。这避免了我们的 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>
于 2015-07-02T11:58:59.230 回答