我正在尝试创建扩展CustomWebServiceMessageReceiverHandlerAdapterorg.springframework.ws.transport.http.WebServiceMessageReceiverHandlerAdapter

WebServiceMessageReceiverHandlerAdapter扩展了实现WebServiceMessageReceiverObjectSupport的抽象InitializingBean

我有一个问题,因为我不明白为什么我必须打电话
自定义处理程序中的afterPropertiesSet()。我未调用此方法而收到错误:“需要工厂消息”。但是,此方法正在抽象类中调用,因此我的自定义处理程序应从抽象类中运行afterPropertiesSet()。如果您知道解决方案,请告诉我。非常感谢。

编辑:这是我的CustomWebServiceMessageReceiverHandlerAdapter

public class CustomWebServiceMessageReceiverHandlerAdapter extends WebServiceMessageReceiverHandlerAdapter {

@Override
protected void handleInvalidXmlException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler,
        InvalidXmlException ex) throws Exception {
    //code
}

@Override
public void afterPropertiesSet() {

}
}


WebServiceMessageReceiverHandlerAdapterWebServiceMessageReceiverObjectSupport来自Spring Framework

public class WebServiceMessageReceiverHandlerAdapter extends WebServiceMessageReceiverObjectSupport{}


没有afterPropertiesSet()

public abstract class WebServiceMessageReceiverObjectSupport implements InitializingBean {

    private WebServiceMessageFactory messageFactory;

    /** Returns the {@code WebServiceMessageFactory}. */
    public WebServiceMessageFactory getMessageFactory() {
        return messageFactory;
    }

    /** Sets the {@code WebServiceMessageFactory}. */
    public void setMessageFactory(WebServiceMessageFactory messageFactory) {
        this.messageFactory = messageFactory;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.notNull(messageFactory, "messageFactory is required");
    }


现在,当我从自定义处理程序中删除afterPropertiesSet()时,将引发异常。我认为我对bean的生命周期一无所知。

最佳答案

我不确定您的具体情况。通常,如果bean实现了InitializingBean,因此实现了afterPropertiesSet,则在实例化bean实例之后和Spring注入所有@Autowired属性/值之后,将调用此方法。

在您的特定情况下,您需要确保(通过继承)类的messageFactory属性已设置。如果您为自动装配提供了合适的设置器,通常这是由Spring完成的:

@Autowired
@Override
public void setMessageFactory(WebServiceMessageFactory messageFactory) {
  super.setMessageFactory(messageFactory);
}


如果在不调用afterPropertiesSet的情况下改写了super.afterPropertiesSet(),则将跳过超级实现的断言,从而可以创建Bean。但是由于messageFactory属性未正确初始化,您可能会进一步遇到问题。

10-06 10:54