考慮一種情況,必須為沒有自定義名稱空間的Spring MVC應用程序配置部分Bean –通常看起來像這樣:
<bean name="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"><property name="webBindingInitializer"><bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"><property name="conversionService" ref="conversionService"></property><property name="validator"><bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/></property></bean></property><property name="messageConverters"><list><bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean><bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean><bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean><bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean><bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean><bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean><bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean></list></property>
</bean><bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"><property name="useSuffixPatternMatch" value="false"></property>
</bean>
在這里,它配置了兩個bean –一個用來處理MVC控制器流的handlerAdapter和一個用來保持請求URI與Controller方法之間的映射以處理請求的handlerMapping。
自定義命名空間“ http://www.springframework.org/schema/mvc”的相同配置變得非常簡潔,通常給其命名空間前綴為“ mvc”:
<mvc:annotation-driven conversion-service="conversionService">
</mvc:annotation-driven>
本質上,這是使用Custom名稱空間的優勢–一種非常簡潔的方式來描述Spring bean定義
那么自定義名稱空間的工作原理是:
Spring Reference文檔中的這一節比我能更好地描述它– http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/extensible-xml.html#extensible-xml -模式。 概括起來,自定義名稱空間包含四個部分:
- 模式 -描述自定義名稱空間的結構-標記名稱,屬性,子標記等。
- 一個NamespaceHandler –為xml元素創建bean定義。 但是,通常由Spring文檔建議的更好的機制是擴展NameSpaceHandlerSupport并為Custom命名空間所支持的不同xml元素注冊一系列BeanDefinitionParser (例如,注解驅動的mvc命名空間的攔截器元素)。
- BeanDefinitionParser –為特定元素創建bean定義–在這里,像<mvc:annotation-driven />這樣的行將擴展為具有實際bean類名的更廣泛的bean定義。
- 注冊架構,NamespaceHandler -用于Spring查找自定義名稱空間的架構,并找到將處理自定義名稱空間的NamespaceHandler。 模式的注冊是通過一個名為META-INF / spring.schemas的文件完成的,這是Spring在類路徑中找到模式而不是通過Web下載模式的一種好方法。 NamespaceHandler使用META-INF / spring.handlers文件進一步指定,并包含將處理自定義名稱空間的NamespaceHandler名稱,例如。 從Spring文檔–
http\://www.mycompany.com/schema/myns=org.springframework.samples.xml.MyNamespaceHandler
綁在一起
有關自定義名稱空間內部工作方式的信息可以很好地理解一些自定義namepsace標記的行為。 考慮一個標簽來加載屬性文件:
<context:property-placeholder location="classpath*:META-INF/spring/database.properties"/>
因此,要查找property-placeholder元素在內部如何工作,請首先找到spring.handlers文件。 由于property-placeholder位于上下文命名空間中,因此spring.conler.s文件將出現在spring-context.jar文件中

文件指示NamespaceHandler為org.springframework.context.config.ContextNamespaceHandler
<context:property-placeholder location="classpath*:META-INF/spring/database.properties" local-override="true" properties-ref="localProperties"/>
在過程中卻變得簡明扼要–
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath*:META-INF/spring/database.properties"></property><property name="localOverride" value="true"></property><property name="properties"><ref bean="localProperties"/></property>
</bean>
但是,這提供了一種很好的方式來理解Spring如何處理自定義命名空間下的某些細微差別。
參考: all和其他博客中來自JCG合作伙伴 Biju Kunjummen的Spring Custom Namespaces 。
翻譯自: https://www.javacodegeeks.com/2012/07/spring-custom-namespaces.html