Why @EnableWs removed aop proxy from spring bean

后端 未结 2 1044
旧巷少年郎
旧巷少年郎 2021-01-15 17:58

I am trying to add custom interceptor in my spring boot web service project. I follow this example and created this config:

package org.example;

import java         


        
2条回答
  •  [愿得一人]
    2021-01-15 18:42

    The problem is the initialization sequence in Spring. Technically, because there is a BeanPostProcessor for the WS Endpoint (AnnotationActionEndpointMapping in spring-ws), it will force the early initialization of any dependencies this needs - especially any EndpointInterceptor beans.

    One way to counter this is to rearrange the BeanPostProcessor's, or even create one of your own, but usually its simpler to stay with the default configuration in Spring - to avoid similar surprises elsewhere in the initialization sequence.

    Perhaps a simpler way to avoid the problem is to use an ObjectFactory in the EndpointInterceptor bean. This will delay instantiating the AppConfig bean until it is referenced, by which time the Aop weaving will also have taken place.

    @Component
    public class CustomValidatingInterceptor extends PayloadValidatingInterceptor {
    
        @Autowired
        private ObjectFactory konfigurace;
    
        @Override
        public boolean handleRequest(MessageContext messageContext, Object endpoint)
                throws IOException, SAXException, TransformerException {
            System.out.println("is config aop proxy in interceptor: " +
                    AopUtils.isAopProxy(konfigurace.getObject()));
            return super.handleRequest(messageContext, endpoint);
        }
    

    Clearly, this then means the CustomValidatingInterceptor must be referenced from WsConfig as an injected (autowired) bean.

    Thanks for the example - there's a fork here that uses the ObjectFactory technique. This showed the config bean as an Aop proxy in all of WsConfig.testAop(), the CountryEndpoint and the CustomValidatingInterceptor when I sent a request in from SoapUI.

提交回复
热议问题