Obtain the Spring context object and the specified Bean object based on the Bundle managed by Spring DM, bundlebean

Source: Internet
Author: User

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!

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.