如何覆盖tapestry AlertManager

时间:2017-03-07 07:44:46

标签: java tapestry

我创建了扩展警报管理器: 接口

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绑定它时:

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复制并粘贴所有方法,但不要扩展它。但可能有更好的方法吗?

2 个答案:

答案 0 :(得分:1)

这是因为当您尝试通过接口类型@Inject时,Tapestry IoC会尝试查找实现接口的所有服务。您的新AlertManagerExt也实现了AlertManager因此含糊不清。

这通常是通过明确指定服务ID或标记注释来解决的,但这不仅适用于您的情况,因为Tapestry的核心代码仅通过接口@Inject AlertManager -- table scheme -- CREATE TABLE Employee (Id int PRIMARY KEY IDENTITY(130620,1),Name varchar(50) NOT NULL) create Table Client(Id int PRIMARY KEY IDENTITY(101,1),Name varchar(50) NOT NULL) create Table Project(Id int PRIMARY KEY IDENTITY(201,1),ClientId intFOREIGN KEY REFERENCES Client(Id),Name varchar(50) not null) create table Timelog(Id int primary key identity(1101,1),EmployeeId int FOREIGN KEY REFERENCES Employee(Id),InTime datetime,OutTime datetime,EmployeeProjectId int FOREIGN KEY REFERENCES EmployeeProject(Id)) create table EmployeeProject(Id int primary key identity(1,1),EmployeeId int FOREIGN KEY REFERENCES Employee(Id),ProjectId int FOREIGN KEY REFERENCES Project(Id)) create table [User]([Id] [int] primary key identity(2101,1),[EmployeeId] int foreign key references Employee(Id),[UserName] [varchar](50),[PassWord] [varchar](50)) --stored procedure-- alter proc EmployeeReport(@empid int) as begin declare @inTime time(0) declare @outTime time(0) declare @fromDate date declare @toDate date declare @clientName varchar(50) declare @projectName varchar(50) set @inTime = (select CAST(InTime as time(0)) from Timelog where EmployeeId=@empid) set @outTime = (select CAST(OutTime as time(0)) from Timelog where EmployeeId = @empid) set @projectName = (select p.Name from Project p inner join EmployeeProject ep on (ep.ProjectId = p.Id and ep.EmployeeId = @empid) where p.Id=ep.ProjectId) set @clientName = (select c.name from client c join Project p on p.Name = @projectName where c.Id = p.ClientId) set @fromDate = (select cast (InTime as date) from Timelog where EmployeeId= @empid) set @toDate = (select cast (outTime as date) from Timelog where EmployeeId= @empid) select @fromDate as FromDate,@toDate as ToDate,c.Name as Client,p.Name as Project ,@inTime as InTime,@outTime as OutTime,t.TotalTime from Timelog t left join Employee e on e.id = t.EmployeeId left join Project p on p.Id = t.EmployeeProjectId left join Client c on c.Id = p.ClientId where t.EmployeeId = @empid print @inTime print @outTime print @fromDate print @toDate print @projectName end

答案 1 :(得分:0)

您可以通过几种方式完成此任务,具体取决于您真正需要的方式。我邀请您阅读documentationIoC cookbook

了解控制反转配置很重要,因为它是一个非常强大的模块。