我在Apache Karaf 3.0.4中工作,我的问题是,当我尝试从数据源(我有一个数据源列表)获取属性时遇到错误,我正在使用此代码
BundleContext bundleContext = FrameworkUtil.getBundle(MyController.class).getBundleContext();
ServiceReference[] serviceReferences =bundleContext.getAllServiceReferences("javax.sql.DataSource", null);
//and in this part
for (ServiceReference serviceReference : serviceReferences) {
Object jndi = serviceReference.getProperty("osgi.jndi.service.name");
if (jndi != null) {
serviceReferenceDataSources.add(serviceReference);
Object service = bundleContext.getService(serviceReference);
//here the code fails
BasicDataSource basicDataSource = (BasicDataSource) service;
}
}
这是堆栈跟踪
java.lang.ClassCastException: Proxyfcb4dd22_6103_4978_b41b_0bacfb118a66 cannot be cast to org.apache.commons.dbcp2.BasicDataSource
我需要BasicDataSource对象来获取其属性(maxTotal,用户名,URL,validationQuery,initialSize,maxWaitMillis)
最后,当我尝试转换为“ javax.sql.DataSource”时,此代码有效,但我需要此对象BasicDataSource。
编辑1
这是我的蓝图
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xsi:schemaLocation="
http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.0.0.xsd
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="${jdbc.initialSize}" />
<property name="maxTotal" value="${jdbc.maxTotal}" />
<property name="maxWaitMillis" value="${jdbc.maxWaitMillis}" />
<property name="validationQuery" value="${jdbc.validationQuery}"/>
<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
</bean>
<service interface="javax.sql.DataSource" ref="dataSource">
<service-properties>
<entry key="osgi.jndi.service.name" value="jdbc/ejemplodb"/>
<entry key="datasource" value="ejemplodb"/>
</service-properties>
</service>
如果我在控制台中打印此代码
...
Object service = bundleContext.getService(serviceReference);
logger.debug("Service getClass() ", service.getClass());
System.out.println("Service in println" + service);
...
我懂了
Service getClass() Proxy79d23ceb_cee2_4031_bdc3_13fd572cdf8c
Service in println org.apache.commons.dbcp2.BasicDataSource@7f753504
希望您能帮助我,谢谢。
最佳答案
根据您的请求,返回的服务将为javax.sql.DataSource类型。在那里,投射到apache的基本数据源无效。但是,只要您只需要数据库连接,就可以使用javax.sql.Datasource吗?
javax.sql.DataSource basicDataSource = (javax.sql.DataSource) service;
如果您需要该服务的类型为BasicDataSource,则可能最终会更改
bundleContext.getAllServiceReferences("javax.sql.DataSource", null);
来查找BasicDatSource
,然后将BasicDataSource的声明更改为BasicDataSource
类型,而不是javax.sql.DataSource
类型。 。