我试图获取对我在AEM 6.4.6中创建的工厂服务的引用,但失败了。这是代码(尝试搜索相似的代码,但是没有找到直接的答案。我可能搜索不够,或者我正在做的事情可能是完全错误的。期待专家的答复)。
更新3 :(最后更新1和2)-这是一个关于如何使用引用获取它的问题。但是,如果专家可以共享其他方法,那也将很好。
服务代码(在第一个捆绑包中)
public interface GodaDataServiceFactory {
List<GodaDataBean> getData();
}
Service Impl Code(另一个捆绑包-第二个捆绑包,请注意其工厂)
@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 Repos
消费者-> https://github.com/GodaProjects/aem646
API-> https://github.com/GodaProjects/api
API IMPL-> https://github.com/GodaProjects/apiImplJcr
更新1:
对于选项1,servlet仍然不满意。
参考godaDataServiceFactory不满意服务名称:com.goda.core.services.GodaDataServiceFactory目标过滤器:(样本=测试)基数:1..1政策:静态政策选项:不愿意没有服务绑定
对于第二个选项,列表保持空白
更新2
消费项目是使用原型13创建的(具有使用工厂服务的servlet)-> https://github.com/GodaProjects/aem646
API项目是使用原型18创建的(具有工厂的API接口)-> https://github.com/GodaProjects/api
API IMPL项目是使用原型18创建的(具有API项目中的API的实现)-> https://github.com/GodaProjects/apiImplJcr
我想我明白了。造成此问题的根本原因似乎是在3个单独的包中具有相同的包结构(aem652,api和apiJcrImpl具有相同的包com.goda.core)。这符合OSGi始终不鼓励使用的拆分束领域。行为也是很多不可预测的。它从不存在的另一个束中寻找类,并抛出“ classnotfound”。真的很混乱。无论如何,这表明他们以一种模式互相掩盖,我既没有时间也没有精力去弄清楚。可以说,我所做的不是很谨慎。所以去。这是解决方案。