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
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.