Level 3
September 21, 2016
Why isn't my value being @Injected?
- September 21, 2016
- 2 replies
- 9720 views
I have this property called "executionID" set on my node.
In Helloworld.html I have this line:
<pre data-sly-use.hello="test.core.models.TestModel">
I then read "hello.value"
My TestModel looks like this:
package test.core.models; //imports... @Model(adaptables = Resource.class) public class TestModel { @Inject @Default(values="0000000000000") private String executionID; public String value; private final Logger logger = LoggerFactory.getLogger(getClass()); @PostConstruct protected void init(){ try{ logger.info("executionID is " + executionID); // more code....
At this point the executionID is always the default, "0000000000000".
How can I inject my value into my class?
If I remove the @Default annotation and it's value, I get this:
org.apache.sling.api.scripting.ScriptEvaluationException: org.apache.sling.scripting.sightly.SightlyException: Identifier test.core.models.TestModel cannot be correctly instantiated by the Use API at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:416) ... Caused by: org.apache.sling.scripting.sightly.SightlyException: Identifier test.core.models.TestModel cannot be correctly instantiated by the Use API at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:89) at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.call(RenderContextImpl.java:66) at org.apache.sling.scripting.sightly.apps.test.components.content.helloworld.SightlyJava_helloworld.render(SightlyJava_helloworld.java:53) at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderUnit.render(RenderUnit.java:54) at org.apache.sling.scripting.sightly.impl.engine.SightlyScriptEngine.evaluateScript(SightlyScriptEngine.java:92) at org.apache.sling.scripting.sightly.impl.engine.SightlyScriptEngine.eval(SightlyScriptEngine.java:78) at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:388) ... 204 more Caused by: org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class test.core.models.TestModel Could not inject private java.lang.String test.core.models.TestModel.executionID caused by No injector returned a non-null value! at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:534) at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:306) at org.apache.sling.models.impl.ModelAdapterFactory.createModel(ModelAdapterFactory.java:200) at org.apache.sling.scripting.sightly.models.impl.SlingModelsUseProvider.provide(SlingModelsUseProvider.java:132) at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:84) ... 210 more