且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 aries 蓝图将 apache karaf 包作为服务注入到 Web 应用程序中?

更新时间:2023-09-06 13:03:58

我在没有使用aries蓝图的情况下解决了上述问题.

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.