You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by Oliver Lietz <ap...@oliverlietz.de> on 2016/04/14 09:42:06 UTC

[Models] Injecting from two sources

Hi,

is it somehow possible to inject both a resource and a request where the 
request is optional and should not break injection when not available?

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
public class MyModel {

    @Inject
    private Resource resource;

    @Self
    @Optional
    private SlingHttpServletRequest request;

}

In my case injection fails with SlingHttpServletRequest not adaptable from 
JcrNodeResource.

Thanks,
O.


Re: [Models] Injecting from two sources

Posted by Konrad Windszus <ko...@gmx.de>.
This may be related to https://issues.apache.org/jira/browse/SLING-4155 <https://issues.apache.org/jira/browse/SLING-4155>.
Please have a look.
Konrad

> On 14 Apr 2016, at 10:27, Oliver Lietz <ap...@oliverlietz.de> wrote:
> 
> On Thursday 14 April 2016 07:47:33 Stefan Seifert wrote:
>> please try to use use @Inject or even better @SlingObject on the "request"
>> variable instead of @Self - then is should work.
>> 
>> for the "resource" parameter i would prefer @SlingObject as well instead of
>> @Inject to make it more explicit and a litte bit more efficient (no need it
>> iterate over all potential injectors internally to find the right match).
> 
> That way it works:
> 
> @Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
> public class MyModel {
> 
>    @SlingObject
>    private Resource resource;
> 
>    @SlingObject(optional = true)
>    private SlingHttpServletRequest request;
> 
> }
> 
> Using @Optional instead of optional = true breaks injection. So there is a 
> problem with @Optional. Can you have a look? Should I file an issue?
> 
> Thanks, Stefan!
> 
> O.
> 
>> stefan
>> 
>>> -----Original Message-----
>>> From: Oliver Lietz [mailto:apache@oliverlietz.de]
>>> Sent: Thursday, April 14, 2016 9:42 AM
>>> To: dev@sling.apache.org
>>> Subject: [Models] Injecting from two sources
>>> 
>>> Hi,
>>> 
>>> is it somehow possible to inject both a resource and a request where the
>>> request is optional and should not break injection when not available?
>>> 
>>> @Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
>>> public class MyModel {
>>> 
>>>   @Inject
>>>   private Resource resource;
>>> 
>>>   @Self
>>>   @Optional
>>>   private SlingHttpServletRequest request;
>>> 
>>> }
>>> 
>>> In my case injection fails with SlingHttpServletRequest not adaptable from
>>> JcrNodeResource.
>>> 
>>> Thanks,
>>> O.
> 


Re: [Models] Injecting from two sources

Posted by Oliver Lietz <ap...@oliverlietz.de>.
On Thursday 14 April 2016 07:47:33 Stefan Seifert wrote:
> please try to use use @Inject or even better @SlingObject on the "request"
> variable instead of @Self - then is should work.
> 
> for the "resource" parameter i would prefer @SlingObject as well instead of
> @Inject to make it more explicit and a litte bit more efficient (no need it
> iterate over all potential injectors internally to find the right match).

That way it works:

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
public class MyModel {

    @SlingObject
    private Resource resource;

    @SlingObject(optional = true)
    private SlingHttpServletRequest request;

}

Using @Optional instead of optional = true breaks injection. So there is a 
problem with @Optional. Can you have a look? Should I file an issue?

Thanks, Stefan!

O.

> stefan
> 
> >-----Original Message-----
> >From: Oliver Lietz [mailto:apache@oliverlietz.de]
> >Sent: Thursday, April 14, 2016 9:42 AM
> >To: dev@sling.apache.org
> >Subject: [Models] Injecting from two sources
> >
> >Hi,
> >
> >is it somehow possible to inject both a resource and a request where the
> >request is optional and should not break injection when not available?
> >
> >@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
> >public class MyModel {
> >
> >    @Inject
> >    private Resource resource;
> >    
> >    @Self
> >    @Optional
> >    private SlingHttpServletRequest request;
> >
> >}
> >
> >In my case injection fails with SlingHttpServletRequest not adaptable from
> >JcrNodeResource.
> >
> >Thanks,
> >O.


RE: [Models] Injecting from two sources

Posted by Stefan Seifert <ss...@pro-vision.de>.
please try to use use @Inject or even better @SlingObject on the "request" variable instead of @Self - then is should work.

for the "resource" parameter i would prefer @SlingObject as well instead of @Inject to make it more explicit and a litte bit more efficient (no need it iterate over all potential injectors internally to find the right match).

stefan


>-----Original Message-----
>From: Oliver Lietz [mailto:apache@oliverlietz.de]
>Sent: Thursday, April 14, 2016 9:42 AM
>To: dev@sling.apache.org
>Subject: [Models] Injecting from two sources
>
>Hi,
>
>is it somehow possible to inject both a resource and a request where the
>request is optional and should not break injection when not available?
>
>@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
>public class MyModel {
>
>    @Inject
>    private Resource resource;
>
>    @Self
>    @Optional
>    private SlingHttpServletRequest request;
>
>}
>
>In my case injection fails with SlingHttpServletRequest not adaptable from
>JcrNodeResource.
>
>Thanks,
>O.
>