2017-01-23 46 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>