3

我正在创建一个 JaxWsPortProxyFactoryBean 实例,并希望在其上设置一个 HandlerResolver 以记录请求。

@Bean(name="serviceName")
public JaxWsPortProxyFactoryBean serviceNameFactory() throws MalformedURLException {
    JaxWsPortProxyFactoryBean factory = new JaxWsPortProxyFactoryBean();
    factory.setHandlerResolver(new CustomHandlerResolver());
}

我还有一个我要引入的公共库,它有一个实现 BeanPostProcessor 并添加它自己的自定义 HandlerResolver 的类:

public class CustomBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(JaxWsPortProxyFactoryBean.class.isAssignableFrom(bean.getClass())) {
            ((JaxWsPortProxyFactoryBean)bean).setHandlerResolver(new AnotherCustomHandlerResolver());
        }
        return bean;
    }
}

问题是postProcessAfterInitialization设置 AnotherCustomHandlerResolver 的步骤吹走了我的 CustomHandlerResolver。

我通过扩展JaxWsPortProxyFactoryBean并使用不同的自定义 HandlerResolver 提出了一种解决方法,该自定义 HandlerResolver 包含多个实例:

JaxWsPortProxyFactoryBean 扩展:

class MultipleHandlerJaxWsPortProxyFactoryBean extends JaxWsPortProxyFactoryBean {

    MultipleHandleResolver resolver;

    public MultipleHandlerJaxWsPortProxyFactoryBean() {
        this.resolver = new MultipleHandleResolver();
        // This instance ALWAYS gets the MultipleHandlerResolver as it's resolver
        super.setHandlerResolver(resolver);
    }

    @Override
    public void setHandlerResolver(HandlerResolver handlerResolver) {
        // When setHandlerResolver is called we keep track of that, but don't set it on super
        this.resolver.addResolver(handlerResolver);
    }
}

多处理程序解析器:

class MultipleHandleResolver implements HandlerResolver {

    List<HandlerResolver> handlerResolvers;

    public MultipleHandleResolver() {
        this.handlerResolvers = new ArrayList<HandlerResolver>();
    }

    /**
     * When we get the handler chain we now iterate over all of our HandlerResolvers to get their handler chains
     */
    @Override
    public List<Handler> getHandlerChain(PortInfo portInfo) {
        List<Handler> handlerList = new ArrayList<Handler>();
        for (HandlerResolver handlerResolver : this.handlerResolvers) {
            handlerList.addAll(handlerResolver.getHandlerChain(portInfo));
        }
        return handlerList;
    }

    public void addResolver(HandlerResolver resolver) {
        this.handlerResolvers.add(resolver);
    }

}

仅仅将处理程序附加到链上似乎很麻烦(而且它还需要将自定义 JaxWsPortProxyFactoryBean 拉入我的 bean 配置中)。是否有内置支持使用 JaxWsPortProxyFactoryBean 执行此操作?

4

0 回答 0