...
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 /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 applicationPath
argument, e.g.:
...
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
}
}
} |