Why isn't my value being @Injected? | Community
Skip to main content
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
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

smacdonald2008
Level 10
September 21, 2016

See this article that shows you how to inject: https://helpx.adobe.com/experience-manager/using/slingmodel_62.html.

smacdonald2008
Level 10
September 21, 2016

There is a working package too. Compare that with your code.

AnkurAhlawat-1
Level 6
September 22, 2016

You can refer this link to Use sling model with Sightly

Create a method in sling model.

public String getExecutionID(){

return executionID;

}

And in your html file use it as hello.executionID

Let me know if you still face any issue.

regards,

Ankur Ahlawat

Level 3
September 22, 2016

I'm not trying to use the variable `executionID` in my html, I'm trying to set the value in java.

 

I'd like to include the property for `executionID` on a node, then @Inject it in a java class. The current problem is that the value always appears as the default. See my code above.

smacdonald2008
Level 10
September 22, 2016

I will be looking at your sample today - its on my TODO list.