Code sample are for illustration purpose only |
@label-This is generally used as a title for the object described by the meta type.
@description-This is generally used as a description for the object described by the meta type.
@metatype-If marked as true it will create a metatype.xml within meta type folder in meta-inf folder in bundle and also it will does a look for the osgi property which is being provided using the property annotation.
@immediate - If marked true will be initialized immediately, if marked as false then the component will check if component is marked as servicefactory which will be covered later in this article.
Let's say you have developed a reusable component and now you want refer the same in another component , so we have only one option , create a new instance of class you want to refer using the new keyword , but that is a serious crime as you are are statically binding a class which is not at all a good design principle. So you can refer another component by converting a component to service using @Service annotation and you can refer the same using@Reference annotation.Just like Spring framework and Google’s Guice it is possible to use dependency injection in AEM in order to acquire a reference to one or more services.
Let's let's see a very basic example of how to make convert a component to service.Once you have marked component with service annotation it get's registered as a service in osgi ecosystem and you can refer service from another component using the reference annotation.
Code snippet are for illustration purpose only |
Code snippet for illustration purpose |
The modified code for Test Component which now refers the Test Service is as shown below line number 30.
Code snippet are for illustration purpose |
Sometimes you need to implement services, which just differ by configuration. A nice example for this is the logging, where you want to have the possibility to have multiple logging facilities being logged to log files at a different level. Somebody (normally the admin of the system) is then able to leverage this and configure the logging as she likes.
So more formally spoken you have zero to N instances of the same service, but just with different configuration. Just duplicating the code and create a logger1 with configurable values, a logger2, logger3 and so doesn’t make sense, as it’s just code duplication and inflexible .
OSGI offers for this kind of problem with the concept of Service Factories. As the name already says, it’s a factory to create services, just by configuration.
So let's modify the TestServiceImpl referred above to make it as service factory, it quite easy just add the configurationFactory=true in @Component annotation.
Code snippet for illustration purpose only |
If you compile and deploy your service now, you can see in your Apache Felix Configuration Manager, that you have a “plus” sign in front of the service; and when you click it, you get a new instance of your service, which you can configure.
But when you have 3 services configured which one do you get when you have something like this:
The answer: It’s not deterministic. You might get any of the configured testservices. If you need a special one, the easiest way is to provide labels for them to make them unique. So add another property to your service:
This will resolve correctly when you have a testservice configured with the name“TestService”. As long as you don’t have such a service, any service containing such a reference won’t start.
Please leave any comments or feed back ,if you have any question
Comments
Post a Comment