...
Code Block |
---|
public class MyClass { @Inject @OsgiService ResourceResolverFactory factory; ... } |
...
In order to use OSGi service support you need to install OsgiToGuiceAutoBindModule module in your injector.
Enhanced injector registration
By convention, Slice reads injector name from second part of a resource type, e.g. if you request a 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 ActicatorActivator, passing 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(); } |
...
Code Block |
---|
@Component
@Service
public class MyComponent implements InjectorListener {
// we need to have this atomic boolean, as the injector may become available
// a few times during component lifecycle
private final AtomicBoolean initialized = new AtomicBoolean();
@Activate
protected void activate() {
initialized.set(false);
}
@Override
public void injectorAvailable(String injectorName) {
if ("myapp".equals(injectorName) && !initialized.getAndSet(true)) {
InjectorWithContext injector = InjectorUtil.getInjector("myapp", getResolver());
// do something clever with the injector
}
}
} |