You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by "Stefan Seifert (JIRA)" <ji...@apache.org> on 2016/06/22 10:01:03 UTC
[jira] [Commented] (SLING-5801) There is no possible to inject
Sling Models adaptable from SlingHttpServletRequest in Sling Model object
which is adapted from SlingHttpServletRequest
[ https://issues.apache.org/jira/browse/SLING-5801?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15344045#comment-15344045 ]
Stefan Seifert commented on SLING-5801:
---------------------------------------
did you try to use the {{@Self}} annotation when injecting instance of class A?
it was designed exactly for this purpose.
> There is no possible to inject Sling Models adaptable from SlingHttpServletRequest in Sling Model object which is adapted from SlingHttpServletRequest
> ------------------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: SLING-5801
> URL: https://issues.apache.org/jira/browse/SLING-5801
> Project: Sling
> Issue Type: Improvement
> Reporter: Piotr Rzasa
> Priority: Minor
>
> Scenario:
> {code}
> @Model(adaptables = SlingHttpServletRequest.class)
> public class A {
> }
> @Model(adaptables = SlingHttpServletRequest.class)
> public class B {
> @Inject
> private A instanceOfA;
> }
> {code}
> Injection of Sling model A in Sling Model B will fails, the error appears that there is no injection which can handle it.
> I think it will be good to add such injector and extend Via annotation to support injection either from resource or from request.
> Here is an example implementation:
> {code}
> @Component
> @Service
> @Property(name = Constants.SERVICE_RANKING, intValue = Integer.MAX_VALUE)
> public class RequestInjector implements Injector {
> public static final String INJECTOR_NAME = "adaptable-from-request-injector";
> @Override
> public String getName() {
> return INJECTOR_NAME;
> }
> @Override
> public Object getValue(Object adaptable, String name, Type declaredType, AnnotatedElement element,
> DisposalCallbackRegistry callbackRegistry) {
> if (!(adaptable instanceof SlingHttpServletRequest)) {
> return null;
> }
> SlingHttpServletRequest request = (SlingHttpServletRequest) adaptable;
> Class<?> clazz = getClassDeclaration(declaredType);
> if (clazz != null) {
> return request.adaptTo(clazz);
> }
> return null;
> }
> private Class<?> getClassDeclaration(Type declaredType) {
> Class<?> result = null;
> if (declaredType instanceof Class<?>) {
> result = (Class<?>) declaredType;
> }
> return result;
> }
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)