gpt4 book ai didi

java - 如何覆盖 Tapestry AlertManager

转载 作者:行者123 更新时间:2023-11-30 06:51:42 26 4
gpt4 key购买 nike

我创建了扩展警报管理器:界面

public interface AlertManagerExt extends AlertManager {
void successCode(String messageCode, Object... args);
void infoCode(String messageCode, Object... args);
void warnCode(String messageCode, Object... args);
void errorCode(String messageCode, Object... args);
}

和实现

public class AlertManagerExtImpl extends AlertManagerImpl implements AlertManagerExt {

private final Messages messages;

public AlertManagerExtImpl(ApplicationStateManager asm, Request request, AjaxResponseRenderer ajaxResponseRenderer,
PerthreadManager perThreadManager, Messages messages) {
super(asm, request, ajaxResponseRenderer, perThreadManager);
this.messages = messages;
}

@Override
public void successCode(String messageCode, Object... args) {
success(getMessage(messageCode, args));
}

@Override
public void infoCode(String messageCode, Object... args) {
info(getMessage(messageCode, args));
}

@Override
public void warnCode(String messageCode, Object... args) {
warn(getMessage(messageCode, args));
}

@Override
public void errorCode(String messageCode, Object... args) {
error(getMessage(messageCode, args));
}

protected String getMessage(String code, Object... args) {
if (args.length > 0) {
return messages.format(code, args);
}
return messages.get(code);
}
}

当我尝试通过 Binder 绑定(bind)它时:

binder.bind(AlertManagerExt.class, AlertManagerExtImpl.class);

我遇到异常:

java.lang.RuntimeException: Service interface org.apache.tapestry5.alerts.AlertManager is matched by 2 services: AlertManager, AlertManagerExtImpl.  Automatic dependency resolution requires that exactly one service implement the interface.
at org.apache.tapestry5.ioc.internal.RegistryImpl.getServiceByTypeAlone(RegistryImpl.java:789)
at org.apache.tapestry5.ioc.internal.RegistryImpl.getServiceByTypeAndMarkers(RegistryImpl.java:797)
at org.apache.tapestry5.ioc.internal.RegistryImpl.getService(RegistryImpl.java:755)

我知道我可以复制并粘贴 AlertManager 中的所有方法并且不扩展它。但可能有更好的方法吗?

最佳答案

这是因为当您尝试仅按接口(interface)类型@Inject 时,Tapestry IoC 会尝试查找实现接口(interface)的所有服务。您的新 AlertManagerExt 也实现了 AlertManager 因此存在歧义。

这通常可以通过显式指定服务 ID 或标记注释来解决,但这不适用于您的情况,因为 Tapestry 的核心代码已经通过接口(interface) @Injecting AlertManager仅。

关于java - 如何覆盖 Tapestry AlertManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42642853/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com