results of code review of mvc namespace
This commit is contained in:
parent
4a5a2b18ac
commit
1bb007c321
|
|
@ -13,7 +13,6 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.web.servlet.config;
|
package org.springframework.web.servlet.config;
|
||||||
|
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
|
|
@ -34,8 +33,8 @@ import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMa
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses the {@code annotated-controllers} element to setup
|
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses the {@code annotation-driven} element to configure
|
||||||
* <code>@Controller</code> configuration in a Spring MVC web application.
|
* a Spring MVC web application.
|
||||||
* <p>
|
* <p>
|
||||||
* Responsible for:
|
* Responsible for:
|
||||||
* <ol>
|
* <ol>
|
||||||
|
|
@ -43,16 +42,14 @@ import org.w3c.dom.Element;
|
||||||
* <li>Registering a AnnotationMethodHandlerAdapter bean for invoking annotated @Controller methods.
|
* <li>Registering a AnnotationMethodHandlerAdapter bean for invoking annotated @Controller methods.
|
||||||
* Will configure the HandlerAdapter's <code>webBindingInitializer</code> property for centrally configuring @Controller DataBinder instances:
|
* Will configure the HandlerAdapter's <code>webBindingInitializer</code> property for centrally configuring @Controller DataBinder instances:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>Configures the conversionService to be the bean named <code>conversionService</code> if such a bean exists,
|
* <li>Configures the conversionService if specified, otherwise defaults to a fresh {@link ConversionService} instance created by the default {@link FormattingConversionServiceFactoryBean}.
|
||||||
* otherwise defaults to a fresh {@link ConversionService} instance created by the default {@link FormattingConversionServiceFactoryBean}.
|
* <li>Configures the validator if specified, otherwise defaults to a fresh {@link Validator} instance created by the default {@link LocalValidatorFactoryBean} <i>if the JSR-303 API is present in the classpath.
|
||||||
* <li>Configures the validator to be the bean named <code>validator</code> if such a bean exists,
|
|
||||||
* otherwise defaults to a fresh {@link Validator} instance created by the default {@link LocalValidatorFactoryBean} <i>if the JSR-303 API is present in the classpath.
|
|
||||||
* </ul>
|
* </ul>
|
||||||
* </ol>
|
* </ol>
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class AnnotatedControllersBeanDefinitionParser implements BeanDefinitionParser {
|
public class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
|
||||||
|
|
||||||
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
||||||
Object source = parserContext.extractSource(element);
|
Object source = parserContext.extractSource(element);
|
||||||
|
|
@ -68,16 +65,18 @@ public class AnnotatedControllersBeanDefinitionParser implements BeanDefinitionP
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// internal helpers
|
||||||
|
|
||||||
private BeanDefinitionHolder registerDefaultAnnotationHandlerMapping(Element element, Object source, ParserContext context) {
|
private BeanDefinitionHolder registerDefaultAnnotationHandlerMapping(Element element, Object source, ParserContext context) {
|
||||||
BeanDefinitionBuilder builder = createBeanBuilder(DefaultAnnotationHandlerMapping.class, source);
|
BeanDefinitionBuilder builder = createBeanBuilder(DefaultAnnotationHandlerMapping.class, source);
|
||||||
builder.addPropertyValue("order", 0);
|
builder.addPropertyValue("order", 0);
|
||||||
return registerBeanDefinition(new BeanDefinitionHolder(builder.getBeanDefinition(), "defaultAnnotationHandlerMapping"), context);
|
return registerBeanDefinition(builder.getBeanDefinition(), context);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BeanDefinitionHolder registerAnnotationMethodHandlerAdapter(Element element, Object source, ParserContext context) {
|
private BeanDefinitionHolder registerAnnotationMethodHandlerAdapter(Element element, Object source, ParserContext context) {
|
||||||
BeanDefinitionBuilder builder = createBeanBuilder(AnnotationMethodHandlerAdapter.class, source);
|
BeanDefinitionBuilder builder = createBeanBuilder(AnnotationMethodHandlerAdapter.class, source);
|
||||||
builder.addPropertyValue("webBindingInitializer", createWebBindingInitializer(element, source, context));
|
builder.addPropertyValue("webBindingInitializer", createWebBindingInitializer(element, source, context));
|
||||||
return registerBeanDefinition(new BeanDefinitionHolder(builder.getBeanDefinition(), "annotationMethodHandlerAdapter"), context);
|
return registerBeanDefinition(builder.getBeanDefinition(), context);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BeanDefinition createWebBindingInitializer(Element element, Object source, ParserContext context) {
|
private BeanDefinition createWebBindingInitializer(Element element, Object source, ParserContext context) {
|
||||||
|
|
@ -88,36 +87,37 @@ public class AnnotatedControllersBeanDefinitionParser implements BeanDefinitionP
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addConversionService(BeanDefinitionBuilder builder, Element element, Object source, ParserContext context) {
|
private void addConversionService(BeanDefinitionBuilder builder, Element element, Object source, ParserContext context) {
|
||||||
if (context.getRegistry().containsBeanDefinition("conversionService")) {
|
if (element.hasAttribute("conversion-service")) {
|
||||||
builder.addPropertyReference("conversionService", "conversionService");
|
builder.addPropertyReference("conversionService", element.getAttribute("conversion-service"));
|
||||||
} else {
|
} else {
|
||||||
builder.addPropertyValue("conversionService", createConversionService(element, source, context));
|
builder.addPropertyValue("conversionService", createDefaultConversionService(element, source, context));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addValidator(BeanDefinitionBuilder builder, Element element, Object source, ParserContext context) {
|
private void addValidator(BeanDefinitionBuilder builder, Element element, Object source, ParserContext context) {
|
||||||
if (context.getRegistry().containsBeanDefinition("validator")) {
|
if (element.hasAttribute("validator")) {
|
||||||
builder.addPropertyReference("validator", "validator");
|
builder.addPropertyReference("validator", element.getAttribute("validator"));
|
||||||
} else {
|
} else {
|
||||||
if (ClassUtils.isPresent("javax.validation.Validator", AnnotatedControllersBeanDefinitionParser.class.getClassLoader())) {
|
if (ClassUtils.isPresent("javax.validation.Validator", AnnotationDrivenBeanDefinitionParser.class.getClassLoader())) {
|
||||||
builder.addPropertyValue("validator", createValidator(element, source, context));
|
builder.addPropertyValue("validator", createDefaultValidator(element, source, context));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private BeanDefinition createConversionService(Element element, Object source, ParserContext context) {
|
private BeanDefinition createDefaultConversionService(Element element, Object source, ParserContext context) {
|
||||||
BeanDefinitionBuilder builder = createBeanBuilder(FormattingConversionServiceFactoryBean.class, source);
|
BeanDefinitionBuilder builder = createBeanBuilder(FormattingConversionServiceFactoryBean.class, source);
|
||||||
return builder.getBeanDefinition();
|
return builder.getBeanDefinition();
|
||||||
}
|
}
|
||||||
|
|
||||||
private BeanDefinition createValidator(Element element, Object source, ParserContext context) {
|
private BeanDefinition createDefaultValidator(Element element, Object source, ParserContext context) {
|
||||||
BeanDefinitionBuilder builder = createBeanBuilder(LocalValidatorFactoryBean.class, source);
|
BeanDefinitionBuilder builder = createBeanBuilder(LocalValidatorFactoryBean.class, source);
|
||||||
return builder.getBeanDefinition();
|
return builder.getBeanDefinition();
|
||||||
}
|
}
|
||||||
|
|
||||||
private BeanDefinitionHolder registerBeanDefinition(BeanDefinitionHolder holder, ParserContext context) {
|
private BeanDefinitionHolder registerBeanDefinition(BeanDefinition definition, ParserContext context) {
|
||||||
context.getRegistry().registerBeanDefinition(holder.getBeanName(), holder.getBeanDefinition());
|
String beanName = context.getReaderContext().generateBeanName(definition);
|
||||||
return holder;
|
context.getRegistry().registerBeanDefinition(beanName, definition);
|
||||||
|
return new BeanDefinitionHolder(definition, beanName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BeanDefinitionBuilder createBeanBuilder(Class<?> clazz, Object source) {
|
private BeanDefinitionBuilder createBeanBuilder(Class<?> clazz, Object source) {
|
||||||
|
|
@ -26,6 +26,6 @@ import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
|
||||||
public class MvcNamespaceHandler extends NamespaceHandlerSupport {
|
public class MvcNamespaceHandler extends NamespaceHandlerSupport {
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
registerBeanDefinitionParser("annotated-controllers", new AnnotatedControllersBeanDefinitionParser());
|
registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenBeanDefinitionParser());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,11 @@
|
||||||
<xsd:import namespace="http://www.springframework.org/schema/tool"
|
<xsd:import namespace="http://www.springframework.org/schema/tool"
|
||||||
schemaLocation="http://www.springframework.org/schema/tool/spring-tool-3.0.xsd" />
|
schemaLocation="http://www.springframework.org/schema/tool/spring-tool-3.0.xsd" />
|
||||||
|
|
||||||
<xsd:element name="annotated-controllers">
|
<xsd:element name="annotation-driven">
|
||||||
<xsd:annotation>
|
<xsd:annotation>
|
||||||
<xsd:documentation
|
<xsd:documentation
|
||||||
source="java:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><![CDATA[
|
source="java:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><![CDATA[
|
||||||
Configures the Spring MVC @Controller programming model.
|
Configures the Spring MVC Annotated Controller programming model.
|
||||||
]]></xsd:documentation>
|
]]></xsd:documentation>
|
||||||
<xsd:appinfo>
|
<xsd:appinfo>
|
||||||
<tool:annotation>
|
<tool:annotation>
|
||||||
|
|
|
||||||
|
|
@ -39,10 +39,10 @@ public class MvcNamespaceTests {
|
||||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(container);
|
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(container);
|
||||||
reader.loadBeanDefinitions(new ClassPathResource("mvc-config.xml", getClass()));
|
reader.loadBeanDefinitions(new ClassPathResource("mvc-config.xml", getClass()));
|
||||||
assertEquals(2, container.getBeanDefinitionCount());
|
assertEquals(2, container.getBeanDefinitionCount());
|
||||||
DefaultAnnotationHandlerMapping mapping = container.getBean("defaultAnnotationHandlerMapping", DefaultAnnotationHandlerMapping.class);
|
DefaultAnnotationHandlerMapping mapping = container.getBean(DefaultAnnotationHandlerMapping.class);
|
||||||
assertNotNull(mapping);
|
assertNotNull(mapping);
|
||||||
assertEquals(0, mapping.getOrder());
|
assertEquals(0, mapping.getOrder());
|
||||||
AnnotationMethodHandlerAdapter adapter = container.getBean("annotationMethodHandlerAdapter", AnnotationMethodHandlerAdapter.class);
|
AnnotationMethodHandlerAdapter adapter = container.getBean(AnnotationMethodHandlerAdapter.class);
|
||||||
assertNotNull(adapter);
|
assertNotNull(adapter);
|
||||||
|
|
||||||
TestController handler = new TestController();
|
TestController handler = new TestController();
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,6 @@
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
|
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
|
||||||
|
|
||||||
<mvc:annotated-controllers/>
|
<mvc:annotation-driven/>
|
||||||
|
|
||||||
</beans>
|
</beans>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue