我正在尝试在我的 Spring Boot Web 服务项目中添加自定义拦截器。我跟随this示例并创建了此配置:
package org.example;
import java.util.List;
import org.aspect.PersistentAspect;
import org.springframework.aop.support.AopUtils;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.server.EndpointInterceptor;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;
@EnableWs
@Configuration
public class WsConfig extends WsConfigurerAdapter {
@Bean
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
final MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean(servlet, "/v1/*");
}
@Bean
public XsdSchema schema() {
return new SimpleXsdSchema(new ClassPathResource("country.xsd"));
}
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
String[] jaxbContext = new String[] { "io.spring.guides.gs_producing_web_service" };
marshaller.setContextPaths(jaxbContext);
return marshaller;
}
@Override
public void addInterceptors(List<EndpointInterceptor> interceptors) {
// aop not working
interceptors.add(new CustomValidatingInterceptor(schema(), config()));
// aop working
// interceptors.add(new CustomValidatingInterceptor(schema(), null));
}
@Bean
public AppConfig config() {
return new AppConfig();
}
@Bean
public PersistentAspect persistentAspect() {
PersistentAspect persistentAspect = new PersistentAspect();
return persistentAspect;
}
@Bean
public Object testAop() {
System.out.println("is config aop proxy: " + AopUtils.isAopProxy(config()));
return null;
}
}
但是当我添加新的拦截器时addInterceptors
方法 我在配置类中删除 aop 代理时遇到问题。知道为什么吗?整个项目已启动git.
问题出在Spring的初始化顺序上。从技术上来说,因为有一个BeanPostProcessor
对于 WS 端点(AnnotationActionEndpointMapping
在 spring-ws 中),它将强制提前初始化所需的任何依赖项 - 特别是任何依赖项EndpointInterceptor
beans.
解决这个问题的一种方法是重新排列 BeanPostProcessor,甚至创建您自己的 BeanPostProcessor,但通常更简单的是保留 Spring 中的默认配置 - 以避免初始化序列中其他地方出现类似的意外情况。
也许避免该问题的更简单方法是使用ObjectFactory
in the EndpointInterceptor
豆。这将延迟实例化AppConfig
bean 直到它被引用,此时 Aop 编织也将发生。
@Component
public class CustomValidatingInterceptor extends PayloadValidatingInterceptor {
@Autowired
private ObjectFactory<AppConfig> 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);
}
显然,这意味着CustomValidatingInterceptor
必须引用自WsConfig
作为注入(自动装配)的 bean。
谢谢你的例子 - 有一个叉子here使用的是ObjectFactory
技术。这表明config
bean 作为 Aop 代理在所有WsConfig.testAop()
, the CountryEndpoint
和CustomValidatingInterceptor
当我从 SoapUI 发送请求时。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)