OSGi-based enterprise-level development framework practices-OSGi Annotations

Source: Internet
Author: User

To use OSGi Annotation in Bundle, you must first define two BeanPostProcessor. We have read the previous article. Here we will take a look at it, as shown in: (figure 1) the above two beans are inherited to org. springframework. beans. factory. config. instantiationAwareBeanPostProcessorAdapter aims to execute additional operations for each Bean in the context of Spring instantiation. Here we want to intercept whether each Bean initialized in Spring context has Annotation of OSGi service, if yes, register the Bean as an OSGi service and register it in the OSGi container. If it is a reference to an OSGi service, it will find the corresponding service in the OSGi container and inject it into the current Bean. In fact, the working principle is relatively simple and easy to understand. However, to use OSGi Annotation in a Bundle, you must define these two backend Bean processors in the Spring XML of the current Bundle. After configuring the two beans, we can write the following code in the class: First, we compile an interface in biz-share Bundle: [java] [java] package org. storevm. helloworld. biz. share;/***** @ author Administrator * @ version $ Id: OsgiServiceTest. java, v 0.1 12:49:00 Administrator Exp $ */public interface OsgiServiceTest {/*** output */void out ();} Then write an implementation class: [java] package org. storevm. helloworld. biz. share; import org. apache. log4j. logger; import org. stor Evm. eosgi. core. annotation. osgiService; import org. storevm. eosgi. core. utils. logUtils;/***** @ author Administrator * @ version $ Id: OsgiServiceTestImpl. java, v 0.1 12:53:24 Administrator Exp $ */@ OsgiService (interfaces = {OsgiServiceTest. class}) public class OsgiServiceTestImpl implements OsgiServiceTest {private static final Logger LOGGER = Logger. getLogger (OsgiServiceTest. class ); /*** @ See org. storevm. helloworld. biz. share. osgiServiceTest # out () */@ Override public void out () {LogUtils.info (LOGGER, "This is an OSGi service, name = {0}", OsgiServiceTest. class) ;}} note that at the beginning of the class, we define the following Annotation: [java] @ OsgiService (interfaces = {OsgiServiceTest. class}) through this Annotation, we can register OsgiServiceTestImpl as an OSGi service. To make the post-Bean processor work, we need to register OsgiServiceTestImpl in the context of Spring, as shown in the following configuration (defined in the bundle-context.xml file ): [html] <bean id = "osgiServiceTest" class = "org. storevm. helloworld. biz. share. osgiServiceTestImpl "/> now, even if our OSGi service is released, we no longer need the <osgi: service/> configuration element. When the OSGi runtime container is started in Eclipse IDE, we can see the following log information displayed in the Console: (Figure 2) We can see that OsgiServiceTest has been successfully released as an OSGi service. However, the problem is not over yet. We just released an OSGi service, but other Bundle still does not know the existence of this OSGi service. To enable other Bundle to use this service, we must expose the service. Open MANIFEST in biz-share Bundle. MF file, and switch to the "Runtime" tab. As shown in: (Figure 3) Click "Add". In the displayed dialog box, select the Package to be exported and click "OK", as shown in figure 4) the final result is as follows: (figure 5) Click the Save button on the toolbar. storevm. helloworld. biz. the share package is exposed and can be introduced by other Bundle. Next, we will introduce the OSGi service released by biz-share Bundle in biz-service-impl Bundle. Open MANIFEST in biz-service-impl Bundle. the MF file is switched to the Dependencies tab, as shown in figure (Figure 6). From figure 6, we can see that biz-share Bundle has been configured as Required Bundle by default by the development framework, therefore, we do not need to import this Bundle any more. Please disable MANIFEST. MF file. Compile an interface in biz-service-impl Bundle as follows: [java] package org. storevm. helloworld. biz. service. impl;/***** @ author Administrator * @ version $ Id: InvokeOsgiServiceTest. java, v 0.1 1:21:37 Administrator Exp $ */public interface InvokeOsgiServiceTest {/*** call */void invoke () ;}then write an implementation class, as shown below: [java] package org. storevm. helloworld. biz. service. impl; import org. storevm. eosgi. core. annotation. se RviceReference; import org. storevm. helloworld. biz. share. osgiServiceTest;/***** @ author Administrator * @ version $ Id: InvokeOsgiServiceTestImpl. java, v 0.1 1:22:45 Administrator Exp $ */public class InvokeOsgiServiceTestImpl implements InvokeOsgiServiceTest {/* OSGi Service */private OsgiServiceTest osgiServiceTest;/*** @ see org. storevm. helloworld. biz. service. impl. invokeOsgiServiceTest # Invoke () */@ Override public void invoke () {osgiServiceTest. out (); // call OSGi service}/*** Setter method for property <tt> osgiServiceTest </tt>. ** @ param osgiServiceTest value to be assigned to property osgiServiceTest */@ ServiceReference public void setOsgiServiceTest (OsgiServiceTest osgiServiceTest) {this. osgiServiceTest = osgiServiceTest;} the focus here is OsgiServiceTest Bean injection. We can see that the Setter method uses the following Annotation: [java] @ ServiceReference through this Annotation, the current Bean will find out if there is a service registered as osgiServiceTest in the OSGi container during instantiation, if yes, the service is injected into the current Bean. If no, an exception is thrown. To verify whether our OSGi service is successfully injected, we define InvokeOsgiServiceTestImpl in the Spring context and define the invoke method as init-method. As shown below (defined in the bundle-context.xml file): [html] <bean class = "org. storevm. helloworld. biz. service. impl. invokeOsgiServiceTestImpl "init-method =" invoke "/> Well, Let's restart the OSGi runtime container in Eclipse IDE. If it has already been started, enter the close command in the Console to close the OSGi container and then start it. In the startup log, we can see the following content: (Figure 7) We can see that the OSGi service is successfully called and we no longer need the <osgi: reference/> configuration element.

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.