Hi,
I'm trying to understand how injectors work on models, so i created the following scenario:
Sling Models Injectors:
script-bindings - org.apache.sling.models.impl.injectors.BindingsInjector
valuemap - org.apache.sling.models.impl.injectors.ValueMapInjector
child-resources - org.apache.sling.models.impl.injectors.ChildResourceInjector
request-attributes - org.apache.sling.models.impl.injectors.RequestAttributeInjector
define-objects - com.adobe.acs.commons.models.injectors.impl.AemObjectsInjector
osgi-services - org.apache.sling.models.impl.injectors.OSGiServiceInjector
I created an adaptable class to both SlingHttpServletRequest.class AND Resource.class :
@Model(adaptables = {SlingHttpServletRequest.class , Resource.class})
public class ModelTest {
protected final Logger logger = LoggerFactory.getLogger(ModelTest.class);
@Inject
@Optional
private String propertyTest;
@Inject
@Optional
private Page currentPage;
@PostConstruct
protected void init() {
logger.debug("Is propertyTest null? {}", propertyTest == null);
logger.debug("Is currentPage null? {}",currentPage == null);
}
}
I'm getting the following result:
18.11.2016 11:06:05.655 *DEBUG* [0:0:0:0:0:0:0:1 [1479427565418] GET /content/test/en/model-test.html HTTP/1.1] com.test.models.ModelTest Is propertyTest null? false
18.11.2016 11:06:05.655 *DEBUG* [0:0:0:0:0:0:0:1 [1479427565418] GET /content/test/en/model-test.html HTTP/1.1] com.test.models.ModelTest Is currentPage null? true
Changing adaptable to SlingHttpServletRequest ONLY:
@Model(adaptables = SlingHttpServletRequest.class)
public class ModelTest {
protected final Logger logger = LoggerFactory.getLogger(ModelTest.class);
@Inject
@Optional
private String propertyTest;
@Inject
@Optional
private Page currentPage;
@PostConstruct
protected void init() {
logger.debug("Is propertyTest null? {}", propertyTest == null);
logger.debug("Is currentPage null? {}",currentPage == null);
}
}
I'm getting the following result:
18.11.2016 11:08:05.733 *DEBUG* [0:0:0:0:0:0:0:1 [1479427565418] GET /content/test/en/model-test.html HTTP/1.1] com.test.models.ModelTest Is propertyTest null? true
18.11.2016 11:08:05.733 *DEBUG* [0:0:0:0:0:0:0:1 [1479427565418] GET /content/test/en/model-test.html HTTP/1.1] com.test.models.ModelTest Is currentPage null? false
Why i can't inject both objects in my model at the same time when i declare my model adaptable to both SlingHttpServletRequest.class and Resource.class?