覆盖库在运行时使用的类

时间:2019-01-08 11:30:56

标签: java spring inheritance override

我想覆盖我的第三方库类之一正在使用的方法。

public abstract class IndirectClient<C extends Credentials, U extends CommonProfile> extends BaseClient<C, U> {
    private LogoutActionBuilder<U> logoutActionBuilder = new NoLogoutActionBuilder<>();

    @Override
    public final RedirectAction getLogoutAction(final WebContext context, final U currentProfile, final String targetUrl) {
        init(context);
        return logoutActionBuilder.getLogoutAction(context, currentProfile, targetUrl);
    }

}

在我的情况下,实现OidcLogoutActionBuilder接口的类LogoutActionBuilder被称为:

public class OidcLogoutActionBuilder<U extends OidcProfile> extends InitializableWebObject implements LogoutActionBuilder<U> {

    ...

    //I'd like to override this method
    @Override
    public RedirectAction getLogoutAction(final WebContext context, final U currentProfile, final String targetUrl) {
        init(context);
    }

}

我尝试扩展OidcLogoutActionBuilder类并覆盖所需的方法:

public class OidcLogoutActionBuilderCustom extends OidcLogoutActionBuilder<OidcProfile> {

        ...

        //Method overriden
        @Override
        public RedirectAction getLogoutAction(final WebContext context, final OidcProfile currentProfile, final String targetUrl) {
            init2(context);
        }
} 

它不起作用,在运行时getLogoutAction()仍然调用OidcLogoutActionBuilder而不是OidcLogoutActionBuilderCustom。我该怎么做呢?

0 个答案:

没有答案