为什么父类中的字段在其他类(Java/apache sling)中使用时具有空值



在我的java代码中,我有以下类:

@Model(adaptables = SlingHttpServletRequest.class)
public class ListModel extends SomeList{
@Inject
protected ResourceResolver resourceResolver;
//this method works fine:
public List<String> getAllData(){
System.out.println(resourceResolver.getUserID());
}
}

当我从这个类调用方法getAllData()时,我看到了我的用户的 id - 这很好。

现在我需要编写一个 2nd 类来扩展上面的类。它看起来像这样:

public class MyNextListModel extends ListModel{
//this method throws nullpointer because resourceResolver is null - why?
@Override
public List<ListItemModel> getAllItems() {
System.out.println(resourceResolver.getUserID());
}
}

为什么第二类中的资源解析器具有空值?

我还尝试移动此代码:

@Inject
protected ResourceResolver resourceResolver;

ListModelMyNextListModel,但即使如此,它在二等舱中仍然是空的

如果从模型继承的类缺少@model注释,则 sling 不知道该怎么做。 只需将注释添加到继承类中,您的代码就可以工作了。顺便说一句,确保另一个类也位于核心Pom中注册为吊索模型包的包中。

最新更新