...
From now on, you can simplify this declarations and Slice will do the work for you. You just need to annotate field or constructor argument with @OsgiService
and Slice will bind such an interface/class to a Peaberry provider , making it injectableautomatically when injector is being created. So your code can just look like this:
Code Block |
---|
public class MyClass { @Inject @OsgiService ResourceResolverFactory factory; ... } |
Injector registration
By convention, Slice reads injector name from second part of a resource type, e.g. if you request a /apps/myapp/mycomponent resource, the injector name will be "myapp". This may be limiting in some cases, especially if you have multiple applications running on a single Sling/AEM instance and you want to group them meaningfully. Since Slice 4.1. you can instruct Slice where to look for an application, e.g. /apps/plugins/my-super-plugin-app. In this case your injector name will be "my-super-plugin-app" instead of "plugins". You can do this by creating InjectorRunner appropriately in your application Acticator, passing applicationPath argument, e.g.:
Code Block |
---|
@Override
public void start(final BundleContext bundleContext) {
final InjectorRunner injectorRunner = new InjectorRunner(bundleContext, INJECTOR_NAME,
"/apps/plugins/my-super-plugin-app", BUNDLE_NAME_FILTER, BASE_PACKAGE);
//modules installation
injectorRunner.start();
} |
Injector listener
Sometimes it's necessary to use Slice bindings during the activation of the OSGi component. It can't be done in the @Activate
method, as there is a race between component activation and the application activator that creates Slice injector - the result is that Slice injector isn't created yet, so OSGi component can't use it. The workaround is to use lazy loading and initialize data on the first request, but it may make the OSGi service design more complex.
...