[英]How to register an org.apache.aries.blueprint.Interceptor in OSGi Karaf?
[英]How to inject apache karaf bundles as a service in the web application using aries blue print?
我有servlet Web應用程序,並希望使用aries藍圖將apache karaf捆綁作為服務注入Web應用程序中。
這些是注入捆綁包的步驟:
1)在blueprint.xml示例代碼中添加了帶有ID和接口值的參考標記
<reference id="jsonStore" interface="com.test.test.jsonstore.service.JsonClientStore" />
2)添加了將ref屬性作為參考ID的bean標簽,該標簽是我們在blueprint.xml文件中注入的束。 示例代碼在這里
<bean id="echo" class="com.test.test.core.jsonrequest.JsonServlet">
<property name="jsonStore" ref="jsonStore"/>
</bean>
3)將blueprint.xml文件的位置作為context-param標記web.xml。 示例代碼在這里
<context-param>
<param-name>blueprintLocation</param-name>
<param-value>OSGI-INF/blueprint/blueprint.xml</param-value>
</context-param>
4)在web.xml中添加了listner類。 示例代碼在這里
<listener>
<listener-class>org.apache.aries.blueprint.web.BlueprintContextListener</listener-class>
</listener>
5)@Inject注釋,用於將特定的bundle注入到servlet類中。 示例代碼在這里
@Inject(ref="jsonStore")
JsonClientStore jsonStore = null;
以下鏈接用於參考文檔http://aries.apache.org/modules/blueprintweb.html
仍然沒有注入束,請有人對此提供幫助嗎?
如何將這些karaf軟件包作為服務注入servlet應用程序?
我不使用白羊座藍圖解決了上述問題。
我在servlet中添加了以下方法來獲取注入的包,我將把包名稱作為serviceName參數發送到以下方法,這將發回所需的注入包服務,方法是使用我將使用該服務中存在的方法。
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);
}
這段代碼解決了我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.