Add more flexible registration of DefaultMethodEndpointAdapter

This commit updates WsConfigurer to get a callback with the list of
default method argument resolvers and return value handlers. Previously,
the callback only allowed to add custom instances and these were added
after the defaults.

Closes gh-1080
This commit is contained in:
Stéphane Nicoll
2025-04-10 15:22:36 +02:00
parent ef00c3e2db
commit 72d79d19db
5 changed files with 143 additions and 68 deletions

View File

@@ -18,82 +18,136 @@ package org.springframework.ws.config.annotation;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ws.config.annotation.WsConfigurationSupportTest.CustomArgumentResolverConfig.MyArgumentResolver;
import org.springframework.ws.config.annotation.WsConfigurationSupportTest.CustomDefaultMethodEndpointAdapterConfig.MyDefaultMethodEndpointAdapter;
import org.springframework.ws.config.annotation.WsConfigurationSupportTest.CustomInterceptorConfig.MyInterceptor;
import org.springframework.ws.config.annotation.WsConfigurationSupportTest.CustomReturnValueHandlerConfig.MyReturnValueHandler;
import org.springframework.ws.server.EndpointInterceptor;
import org.springframework.ws.server.endpoint.adapter.DefaultMethodEndpointAdapter;
import org.springframework.ws.server.endpoint.adapter.method.MethodArgumentResolver;
import org.springframework.ws.server.endpoint.adapter.method.MethodReturnValueHandler;
import org.springframework.ws.server.endpoint.adapter.method.SourcePayloadMethodProcessor;
import org.springframework.ws.server.endpoint.interceptor.EndpointInterceptorAdapter;
import org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link WsConfigurationSupport}.
*
* @author Arjen Poutsma
* @author Stephane Nicoll
*/
public class WsConfigurationSupportTest {
class WsConfigurationSupportTest {
private ApplicationContext applicationContext;
@Test
void interceptors() {
try (ConfigurableApplicationContext applicationContext = load(CustomInterceptorConfig.class)) {
PayloadRootAnnotationMethodEndpointMapping endpointMapping = applicationContext
.getBean(PayloadRootAnnotationMethodEndpointMapping.class);
assertThat(endpointMapping.getOrder()).isEqualTo(0);
EndpointInterceptor[] interceptors = endpointMapping.getInterceptors();
assertThat(interceptors).singleElement().isInstanceOf(MyInterceptor.class);
}
}
@BeforeEach
public void setUp() {
@Test
void argumentResolvers() {
try (ConfigurableApplicationContext applicationContext = load(CustomArgumentResolverConfig.class)) {
DefaultMethodEndpointAdapter bean = applicationContext.getBean(DefaultMethodEndpointAdapter.class);
List<MethodArgumentResolver> methodArgumentResolvers = bean.getMethodArgumentResolvers();
assertThat(methodArgumentResolvers).hasSizeGreaterThan(1).element(0).isInstanceOf(MyArgumentResolver.class);
}
}
@Test
void returnValueHandlers() {
try (ConfigurableApplicationContext applicationContext = load(CustomReturnValueHandlerConfig.class)) {
DefaultMethodEndpointAdapter bean = applicationContext.getBean(DefaultMethodEndpointAdapter.class);
List<MethodReturnValueHandler> methodReturnValueHandlers = bean.getMethodReturnValueHandlers();
assertThat(methodReturnValueHandlers).hasSizeGreaterThan(1)
.element(0)
.isInstanceOf(MyReturnValueHandler.class);
}
}
@Test
void defaultMethodEndpointAdapter() {
try (ConfigurableApplicationContext applicationContext = load(CustomDefaultMethodEndpointAdapterConfig.class)) {
assertThat(applicationContext.getBean(DefaultMethodEndpointAdapter.class))
.isInstanceOf(MyDefaultMethodEndpointAdapter.class);
}
}
private ConfigurableApplicationContext load(Class<?>... componentClasses) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(TestConfig.class);
applicationContext.register(componentClasses);
applicationContext.refresh();
this.applicationContext = applicationContext;
return applicationContext;
}
@Test
public void interceptors() {
PayloadRootAnnotationMethodEndpointMapping endpointMapping = this.applicationContext
.getBean(PayloadRootAnnotationMethodEndpointMapping.class);
assertThat(endpointMapping.getOrder()).isEqualTo(0);
EndpointInterceptor[] interceptors = endpointMapping.getInterceptors();
assertThat(interceptors).hasSize(1);
assertThat(interceptors[0]).isInstanceOf(MyInterceptor.class);
}
@Test
public void defaultMethodEndpointAdapter() {
DefaultMethodEndpointAdapter endpointAdapter = this.applicationContext
.getBean(DefaultMethodEndpointAdapter.class);
assertThat(endpointAdapter).isNotNull();
assertThat(endpointAdapter).isInstanceOf(MyDefaultMethodEndpointAdapter.class);
}
@Configuration
public static class TestConfig extends WsConfigurationSupport {
@Configuration(proxyBeanMethods = false)
static class CustomInterceptorConfig extends WsConfigurationSupport {
@Override
protected void addInterceptors(List<EndpointInterceptor> interceptors) {
interceptors.add(new MyInterceptor());
}
static class MyInterceptor extends EndpointInterceptorAdapter {
}
}
@Configuration(proxyBeanMethods = false)
static class CustomArgumentResolverConfig extends WsConfigurationSupport {
@Override
protected void addArgumentResolvers(List<MethodArgumentResolver> argumentResolvers) {
assertThat(argumentResolvers).isNotEmpty();
argumentResolvers.add(0, new MyArgumentResolver());
}
static class MyArgumentResolver extends SourcePayloadMethodProcessor {
}
}
@Configuration(proxyBeanMethods = false)
static class CustomReturnValueHandlerConfig extends WsConfigurationSupport {
@Override
protected void addReturnValueHandlers(List<MethodReturnValueHandler> returnValueHandlers) {
assertThat(returnValueHandlers).isNotEmpty();
returnValueHandlers.add(0, new MyReturnValueHandler());
}
static class MyReturnValueHandler extends SourcePayloadMethodProcessor {
}
}
@Configuration(proxyBeanMethods = false)
static class CustomDefaultMethodEndpointAdapterConfig extends WsConfigurationSupport {
@Bean
@Override
public DefaultMethodEndpointAdapter defaultMethodEndpointAdapter() {
return new MyDefaultMethodEndpointAdapter();
}
}
static class MyDefaultMethodEndpointAdapter extends DefaultMethodEndpointAdapter {
public static class MyInterceptor extends EndpointInterceptorAdapter {
}
public static class MyDefaultMethodEndpointAdapter extends DefaultMethodEndpointAdapter {
}
}

View File

@@ -38,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Arjen Poutsma
*/
@Deprecated
public class WsConfigurerAdapterTest {
private ApplicationContext applicationContext;
@@ -72,9 +73,9 @@ public class WsConfigurerAdapterTest {
DefaultMethodEndpointAdapter endpointAdapter = this.applicationContext
.getBean(DefaultMethodEndpointAdapter.class);
List<MethodArgumentResolver> argumentResolvers = endpointAdapter.getCustomMethodArgumentResolvers();
List<MethodArgumentResolver> argumentResolvers = endpointAdapter.getMethodArgumentResolvers();
assertThat(argumentResolvers).hasSize(1);
assertThat(argumentResolvers).hasSizeGreaterThan(1);
assertThat(argumentResolvers.get(0)).isInstanceOf(MyMethodArgumentResolver.class);
argumentResolvers = endpointAdapter.getMethodArgumentResolvers();
@@ -88,9 +89,9 @@ public class WsConfigurerAdapterTest {
DefaultMethodEndpointAdapter endpointAdapter = this.applicationContext
.getBean(DefaultMethodEndpointAdapter.class);
List<MethodReturnValueHandler> returnValueHandlers = endpointAdapter.getCustomMethodReturnValueHandlers();
List<MethodReturnValueHandler> returnValueHandlers = endpointAdapter.getMethodReturnValueHandlers();
assertThat(returnValueHandlers).hasSize(1);
assertThat(returnValueHandlers).hasSizeGreaterThan(1);
assertThat(returnValueHandlers.get(0)).isInstanceOf(MyReturnValueHandler.class);
returnValueHandlers = endpointAdapter.getMethodReturnValueHandlers();
@@ -109,12 +110,12 @@ public class WsConfigurerAdapterTest {
@Override
public void addArgumentResolvers(List<MethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new MyMethodArgumentResolver());
argumentResolvers.add(0, new MyMethodArgumentResolver());
}
@Override
public void addReturnValueHandlers(List<MethodReturnValueHandler> returnValueHandlers) {
returnValueHandlers.add(new MyReturnValueHandler());
returnValueHandlers.add(0, new MyReturnValueHandler());
}
}