2017-01-23 118 views
1

我有一個包含部署到Apache ServiceMix的數據源的藍圖文件。我能夠從Apache Karaf控制檯查詢數據源。我如何從一個Camel Spring-DM bundle應用程序訪問這個數據源?這是我的藍圖文件:從OSGi包訪問數據源

<?xml version="1.0" encoding="UTF-8"?> 
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> 
    <bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource"> 
     <property name="URL" value="URL"/> 
     <property name="user" value="USER"/> 
     <property name="password" value="PASSWORD"/> 
    </bean> 
    <service interface="javax.sql.DataSource" ref="dataSource" id="ds"> 
    <service-properties> 
      <entry key="osgi.jndi.service.name" value="jdbc/ds"/> 
    </service-properties> 
    </service> 
</blueprint> 

回答

1

您可以將DataSource綁定爲OSGi服務。在春天DM這是osgi:參考,在藍圖中它將是參考。

<reference id="dataSource" interface="javax.sql.DataSource"/> 

然後,您可以將DataSource注入到SqlComponent中。

作爲示例,請參閱我爲this camel route所做的修復。這是藍圖,但對於春天來說幾乎是一樣的。

<bean id="sql" class="org.apache.camel.component.sql.SqlComponent"> 
    <property name="dataSource" ref="dataSource"/> 
</bean> 
+0

你能提供給我的如何注入數據源的例子。我無法解決我的應用程序包中的DataSource。 – user6641655

1

使用Hibernate作爲JPA提供:

<?xml version="1.0" encoding="UTF-8"?> 

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
           http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" 
      version="2.1"> 

    <persistence-unit name="jpa" transaction-type="JTA"> 
     <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
     <jta-data-source>osgi:service/jdbc/ds</jta-data-source> 

    ... 

    </persistence-unit> 
</persistence>