Obtain the Spring context object and the specified Bean object based on the Bundle managed by Spring DM, bundlebean
In the essay about service registration and reference, context is mentioned. the getServiceReferences () method obtains the specified type of service reference in the OSGI framework container to obtain the corresponding service object. At the same time, this method can also get the Spring context object in the Bundle through the Bundle-SymbolicName name. Similarly, based on the Spring context object, we can also get the corresponding service object (service object, is a Bean object in Spring)
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 ("corresponding service not found"); continue;} ApplicationContext springContext = (ApplicationContext) context. getService (ref [0]); System. out. println (springContext. getBean (beanID ));
}
In the above Code, apart from introducing the required package automatically prompted by Eclipse, you will see an error in the springContext. getBean (beanID) line of code.
The cause of this problem must be that the dependent packages are not imported. Anyone familiar with Spring knows that the getBean () method is in the BeanFactory class.
Therefore, we need to manually introduce the two dependent Packages in the OSGI development environment, open the MANIFEST. MF file, select the Dependencies tab, and click Add to retrieve the corresponding Packages name.
After successful introduction, the error will be solved!