我正在尝试获取对我创建的工厂服务的引用(在 AEM 6.4.6 中)并且失败了。这是代码(确实尝试搜索类似的,但没有找到直接的答案。我可能搜索得不够多,或者我正在做的事情可能完全错误。期待专家的回复)。
更新 3:(最后更新 1 和 2) - 这是一个关于如何使用引用获得它的问题。但是,如果专家可以分享其他方法,那也很棒。
服务代码(在第一个包中)
public interface GodaDataServiceFactory {
List<GodaDataBean> getData();
}
服务实现代码(另一个捆绑包 - 第二个捆绑包,注意它是一个工厂)
@Component(
service = GodaDataServiceFactory.class,
factory = "GodaDataServiceFactory",
configurationPolicy = ConfigurationPolicy.REQUIRE
)
@Designate(ocd = GodaDataServiceFactoryConfig.class, factory = true)
public class GodaDataServiceFactoryJcrImpl implements GodaDataServiceFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(GodaDataServiceFactoryJcrImpl.class);
@Override
public List<GodaDataBean> getData() {
return null;
}
}
参考
选项 1(直接参考):
@Reference(target = "(sample=test)")
private GodaDataServiceFactory godaDataServiceFactory;
选项 2(间接使用绑定和取消绑定)
@Reference(
name = "godaDataServiceFactory",
cardinality = ReferenceCardinality.MULTIPLE,
policy = ReferencePolicy.DYNAMIC,
bind = "bindGodaDataServiceFactory",
unbind = "unbindGodaDataServiceFactory")
List<GodaDataServiceFactory> godaDataServiceFactoryList = new ArrayList<>();
protected synchronized void bindGodaDataServiceFactory(final GodaDataServiceFactory config) {
LOGGER.info("Goda config factory: {}", config);
godaDataServiceFactoryList.add(config);
}
protected synchronized void unbindGodaDataServiceFactory(final GodaDataServiceFactory config) {
godaDataServiceFactoryList.remove(config);
}
这些似乎都不起作用。在第一种情况下,godaDataServiceFactory 为空。第二种情况,列表总是空的。请注意,消费者是一个 servlet。
我的 GitHub 存储库
消费者-> https://github.com/GodaProjects/aem646
API -> https://github.com/GodaProjects/api
API IMPL -> https://github.com/GodaProjects/apiImplJcr
更新1:
对于选项 1,servlet 仍然不满意。
参考 godaDataServiceFactory
Unsatisfied Service Name: com.goda.core.services.GodaDataServiceFactory Target Filter: (sample=test) Cardinality: 1..1 Policy: static Policy Option: relucant No Services bound
对于第二个选项,列表保持为空
更新 2
消费者项目是使用 Archetype 13 创建的(具有使用工厂服务的 servlet)-> https://github.com/GodaProjects/aem646
API 项目是使用 Archetype 18 创建的(具有工厂的 API 接口)-> https://github.com/GodaProjects/api
API IMPL 项目是使用 Archetype 18 创建的(具有 API 项目中 API 的实现)-> https://github.com/GodaProjects/apiImplJcr