How to inject apache karaf bundles as a service in the web application using aries blue print?

有些话、适合烂在心里 提交于 2019-12-11 10:37:27

问题


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) added reference tag with id and interface values in the blueprint.xml sample code is here

<reference id="jsonStore" interface="com.test.test.jsonstore.service.JsonClientStore" />

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 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) 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 annotation for injecting the particular bundle in the servlet class. sample code is here

@Inject(ref="jsonStore")
    JsonClientStore jsonStore = null;

The following link is for reference documentation http://aries.apache.org/modules/blueprintweb.html

Still the bundles are not injected please some one can help on this ?

how to inject these karaf bundles as a service to the servlet application?


回答1:


I solved the above problem without using aries blueprint.

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.



来源:https://stackoverflow.com/questions/51930549/how-to-inject-apache-karaf-bundles-as-a-service-in-the-web-application-using-ari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!