Getting Spring Context Object and Specified Bean Object by Bundle Based on Spring DM Management

Keywords: Java Spring Eclipse

  In the essay on service registration and reference, it is mentioned that context.getServiceReferences()Method, which can be obtained by this method OSGI The specified type of service reference in the framework container is used to obtain the corresponding service object. At the same time, the method can also pass through Bundle-SymbolicName Name gets the Bundle Medium Spring Context objects, also based on Spring Context object, we can also get the corresponding service object very well.(The service object is Spring One of them Bean object)

String callName = "com.sample.service.IHelloService.sayHello(msg)";
        String bundleSymbolicName = "com.sample.service.impl";
        bundleSymbolicName = "com.sample.service";
        String beanID = "com.sample.service.IHelloService";
        try {
            System.out.println(ApplicationContext.class.getName());
            
            Bundle[] bundles = context.getBundles();
            for (Bundle bundle : bundles) {
                if(bundle.getSymbolicName().startsWith(bundleSymbolicName)){
                    
                    
                        System.out.println(bundle.getSymbolicName());
                        ServiceReference[] ref = context.getServiceReferences(ApplicationContext.class.getName(), "(Bundle-SymbolicName=" + bundle.getSymbolicName() + ")");
                        if (ref == null || ref.length == 0) {
                            System.out.println("No corresponding service was found");
                            continue;
                        }
                        
                        ApplicationContext springContext = (ApplicationContext) context.getService(ref[0]);
                        System.out.println(springContext.getBean(beanID));
          }

 

In the above code, in addition to introducing the package required for Eclipse autoprompt, you will see errors in the line springContext. getBean (bean ID)

 

The reason for this problem must be that there are also packages that need to be relied on that have not been imported. Friends familiar with Spring know that the getBean() method is in the BeanFactory class

Therefore, we need to manually introduce two Packages in OSGI development environment, open the MANIFEST.MF file, select the Dependencies tab, click Add, and retrieve the corresponding package name to achieve the introduction.

 

 

After successful introduction, the mistakes will be solved!

Posted by hame22 on Mon, 11 Feb 2019 17:33:17 -0800