diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java index a2a8b61596..a3d1a26c53 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java @@ -853,6 +853,7 @@ public class MvcNamespaceTests { ContentNegotiationManager manager = (ContentNegotiationManager) accessor.getPropertyValue(beanName); assertNotNull(manager); assertSame(manager, this.appContext.getBean(ContentNegotiationManager.class)); + assertSame(manager, this.appContext.getBean("mvcContentNegotiationManager")); } @Test diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParser.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParser.java index 6e31e25dc8..c234f37077 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParser.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -109,6 +109,9 @@ class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser { private static final boolean jackson2Present = ClassUtils.isPresent( "com.fasterxml.jackson.databind.ObjectMapper", MessageBrokerBeanDefinitionParser.class.getClassLoader()); + private static final boolean javaxValidationPresent = + ClassUtils.isPresent("javax.validation.Validator", MessageBrokerBeanDefinitionParser.class.getClassLoader()); + @Override public BeanDefinition parse(Element element, ParserContext context) { @@ -516,6 +519,11 @@ class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser { beanDef.getPropertyValues().add("pathMatcher", new RuntimeBeanReference(pathMatcherRef)); } + RuntimeBeanReference validatorRef = getValidator(messageBrokerElement, source, context); + if (validatorRef != null) { + beanDef.getPropertyValues().add("validator", validatorRef); + } + Element resolversElement = DomUtils.getChildElementByTagName(messageBrokerElement, "argument-resolvers"); if (resolversElement != null) { values.add("customArgumentResolvers", extractBeanSubElements(resolversElement, context)); @@ -529,6 +537,24 @@ class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser { registerBeanDef(beanDef, context, source); } + private RuntimeBeanReference getValidator(Element messageBrokerElement, Object source, ParserContext parserContext) { + if (messageBrokerElement.hasAttribute("validator")) { + return new RuntimeBeanReference(messageBrokerElement.getAttribute("validator")); + } + else if (javaxValidationPresent) { + RootBeanDefinition validatorDef = new RootBeanDefinition( + "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean"); + validatorDef.setSource(source); + validatorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); + String validatorName = parserContext.getReaderContext().registerWithGeneratedName(validatorDef); + parserContext.registerComponent(new BeanComponentDefinition(validatorDef, validatorName)); + return new RuntimeBeanReference(validatorName); + } + else { + return null; + } + } + private ManagedList extractBeanSubElements(Element parentElement, ParserContext parserContext) { ManagedList list = new ManagedList(); list.setSource(parserContext.extractSource(parentElement)); diff --git a/spring-websocket/src/main/resources/org/springframework/web/socket/config/spring-websocket-4.3.xsd b/spring-websocket/src/main/resources/org/springframework/web/socket/config/spring-websocket-4.3.xsd index 691d1b0122..3d5eb9e9e4 100644 --- a/spring-websocket/src/main/resources/org/springframework/web/socket/config/spring-websocket-4.3.xsd +++ b/spring-websocket/src/main/resources/org/springframework/web/socket/config/spring-websocket-4.3.xsd @@ -21,7 +21,7 @@ @@ -916,9 +916,16 @@ + ]]> + + + + + diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParserTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParserTests.java index aa52922b2c..824cba62e5 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParserTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParserTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,6 +60,8 @@ import org.springframework.mock.web.test.MockServletContext; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.util.MimeTypeUtils; +import org.springframework.validation.Errors; +import org.springframework.validation.Validator; import org.springframework.web.HttpRequestHandler; import org.springframework.web.context.support.GenericWebApplicationContext; import org.springframework.web.servlet.HandlerMapping; @@ -356,6 +358,14 @@ public class MessageBrokerBeanDefinitionParserTests { public void customChannels() { loadBeanDefinitions("websocket-config-broker-customchannels.xml"); + SimpAnnotationMethodMessageHandler annotationMethodMessageHandler = + this.appContext.getBean(SimpAnnotationMethodMessageHandler.class); + + Validator validator = annotationMethodMessageHandler.getValidator(); + assertNotNull(validator); + assertSame(this.appContext.getBean("myValidator"), validator); + assertThat(validator, Matchers.instanceOf(TestValidator.class)); + List> subscriberTypes = Arrays.>asList(SimpAnnotationMethodMessageHandler.class, UserDestinationMessageHandler.class, SimpleBrokerMessageHandler.class); @@ -520,3 +530,13 @@ class TestWebSocketHandlerDecorator extends WebSocketHandlerDecorator { class TestStompErrorHandler extends StompSubProtocolErrorHandler { } + +class TestValidator implements Validator { + @Override + public boolean supports(Class clazz) { + return false; + } + + @Override + public void validate(Object target, Errors errors) { } +} diff --git a/spring-websocket/src/test/resources/org/springframework/web/socket/config/websocket-config-broker-customchannels.xml b/spring-websocket/src/test/resources/org/springframework/web/socket/config/websocket-config-broker-customchannels.xml index e8908da1a1..fdf470495e 100644 --- a/spring-websocket/src/test/resources/org/springframework/web/socket/config/websocket-config-broker-customchannels.xml +++ b/spring-websocket/src/test/resources/org/springframework/web/socket/config/websocket-config-broker-customchannels.xml @@ -4,7 +4,7 @@ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd"> - + @@ -31,4 +31,6 @@ + +