问题描述
我有servlet Web应用程序,并希望使用白羊座蓝图将apache karaf捆绑作为服务注入Web应用程序中.
I have servlet web application and want to inject apache karaf bundles as a service in the web application using aries blueprint.
以下是注入捆绑包的步骤:
These are the Steps followed for injecting the bundles:
1)在blueprint.xml中添加了具有ID和接口值的参考标记示例代码在这里
1) added reference tag with id and interface values in the blueprint.xmlsample code is here
<reference id="jsonStore" interface="com.test.test.jsonstore.service.JsonClientStore" />
2)添加了将ref属性作为参考ID的bean标记,该标记与我们在blueprint.xml文件中注入的内容捆绑在一起.示例代码在这里
2) added bean tag with ref attribute as reference id, of bundles what we are injecting in the blueprint.xml file.sample code is here
<bean id="echo" class="com.test.test.core.jsonrequest.JsonServlet">
<property name="jsonStore" ref="jsonStore"/>
</bean>
3)blueprint.xml文件的位置作为context-param标记了web.xml.示例代码在这里
3) blueprint.xml file location as context-param tag the web.xml.sample code is here
<context-param>
<param-name>blueprintLocation</param-name>
<param-value>OSGI-INF/blueprint/blueprint.xml</param-value>
</context-param>
4)在web.xml中添加了listner类.示例代码在这里
4) added listner class in the web.xml.sample code is here
<listener>
<listener-class>org.apache.aries.blueprint.web.BlueprintContextListener</listener-class>
</listener>
5)@Inject注释,用于将特定的bundle注入到servlet类中.示例代码在这里
5) @Inject annotation for injecting the particular bundle in the servlet class.sample code is here
@Inject(ref="jsonStore")
JsonClientStore jsonStore = null;
以下链接用于参考文档 http://aries.apache.org/modules/blueprintweb.html
The following link is for reference documentationhttp://aries.apache.org/modules/blueprintweb.html
仍然没有注入束,请有人可以帮忙吗?
Still the bundles are not injected please some one can help on this ?
如何将这些karaf软件包作为服务注入servlet应用程序?
how to inject these karaf bundles as a service to the servlet application?
推荐答案
我在不使用白羊座蓝图的情况下解决了上述问题.
I solved the above problem without using aries blueprint.
我在servlet中添加了以下方法来获取注入的包,我将把包名称作为serviceName参数发送到以下方法,这将发回所需的注入包服务,通过使用我将使用当前方法在该服务中.
The following method i added in the servlet to get injected bundles, i will send the bundle name as serviceName parameter to the below method, this will send back the required service of injected bundle, by using that i will use the methods present in that service.
private <T> T getService(Class<T> serviceName) {
Bundle bundle = FrameworkUtil.getBundle(serviceName);
if ( bundle == null ) {
return null;
}
BundleContext bundleContext = bundle.getBundleContext();
ServiceReference serviceRef = bundleContext.getServiceReference(serviceName);
if (serviceRef == null)
return null;
return (T) bundleContext.getService(serviceRef);
}
这段代码解决了我的问题.
This code solved my problem.
这篇关于如何使用aries蓝图将apache karaf软件包作为服务注入Web应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!