首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用spring云命名空间和两个DataSources

使用spring云命名空间和两个DataSources
EN

Stack Overflow用户
提问于 2015-06-14 09:55:40
回答 2查看 1.3K关注 0票数 0

我有一个组件,我正在更新它,以便在私有PCF中运行。我在应用程序中定义了两个DataSources和一个RabbitMQ连接工厂。

我看到Thomas的一篇关于使用云命名空间和同时处理多个服务的文章-- https://spring.io/blog/2011/11/09/using-cloud-foundry-services-with-spring-part-3-the-cloud-namespace。这是通过使用@Autowired和@限定符注释来处理的。

我想知道如何实现这一点,但当我们不是“自动发条”和“@限定符”注释时,例如将一个DataSource连接到一个JdbcTemplate。在这里,我们无法指定@限定符注释。

我的应用程序是基于Spring配置的。我确实有能力在其中一个DataSources上使用@Au牛毛和@限定符注释,但另一个是JPA实体管理器。请参阅代码片段。

任何帮助都是非常感谢的。

代码语言:javascript
复制
    <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_buildpack_offline :java_buildpack_offline Java -buildpack-脱机-v2.4.zip Spring自动重新配置版本1.4.0。

更新:--这是两个数据源的完整配置,包括使用DAO从数据源加载属性的PropertySourcesPlaceholderConfigurer。

代码语言:javascript
复制
<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的自动重新配置。

EN

回答 2

Stack Overflow用户

发布于 2015-06-16 15:55:58

首先,Thomas的文章很古老,引用了一个不受欢迎的支持库。不应该使用org.cloudfoundry:cloudfoundry-runtime:0.8.1依赖项,而应该使用Spring连接器依赖项

然后,您可以按照提供的指示使用使用XML。对于相同类型的多个服务,您需要为每个bean指定服务名称。按照您的示例,假设您创建了两个名为inventory-dbcustomer-db的CF数据库服务,这可能如下所示:

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2015-07-02 11:58:59

我已经通过使用spring使用的工厂bean来解决这个问题:数据源,CloudDataSourceFactory。创建此服务的实例并连接配置,包括CF服务的service-name。这避免了PropertySourcesPlaceholderConfigurer在定义bean之前就试图使用数据源的问题。

代码语言:javascript
复制
    <!--
        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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30828027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档