Jdr*_*uwe 2 adobe osgi annotations sling aem
我在Sling模型中的javax.annotation.PostConstruct注释存在问题。
使用我的模型的html文件:
<div data-sly-use="com.company.platform.component.general.textblockvalidator.TextBlockValidatorModel" data-sly-unwrap />
Run Code Online (Sandbox Code Playgroud)
模型:
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
@Model(adaptables = org.apache.sling.api.resource.Resource.class)
public class TextBlockValidatorModel {
@PostConstruct
private void init() {
System.out.println();
}
public String getValidate(){
return "This works";
}
}
Run Code Online (Sandbox Code Playgroud)
我可以从看得见的文件中调用getter,但似乎从未输入过@PostConstruct init()方法。
IntelliJ确实给了我关于注释的警告,但是我不确定自己做错了什么:
吊索模型包装:
<Sling-Model-Packages>
...
com.asadventure.platform.component
...
</Sling-Model-Packages>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?提前致谢!
首先,通过在以下网页中查找您的课程来检查Sling模型是否已正确注册: http:// localhost:4502 / system / console / status-adapters
如果未在此处列出,则很可能尚未指定的<Sling-Model-Packages>属性maven-bundle-plugin。
我还将尝试将方法的访问修饰符更改init为protected或public。
更新:
我为AEM 6.1创建了一个示例项目,演示了@PostConstruct批注的使用。
Sling Model类:
@Model(adaptables = Resource.class)
public class SampleModel {
private boolean postContructCalled = false;
@PostConstruct
public void init() {
this.postContructCalled = true;
}
public boolean isPostContructCalled() {
return this.postContructCalled;
}
}
Run Code Online (Sandbox Code Playgroud)
还有一个简单的HTL组件:
<sly data-sly-use.model="com.github.mickleroy.models.SampleModel">
<p>@PostConstruct was called: ${model.postContructCalled}</p>
</sly>
Run Code Online (Sandbox Code Playgroud)
请注意data-sly-use指令的使用-您需要提供型号名称。
另外,正如我在评论中提到的那样,您不应将其添加javax.annotation-api为依赖项,因为它是JDK的一部分。
完整的源代码在这里:https://github.com/mickleroy/sling-models-sample
| 归档时间: |
|
| 查看次数: |
3340 次 |
| 最近记录: |