Spring namespace and assembly properties using spring namespace p

Spring namespace and assembly properties using spring namespace p

This comes from XML. Spring's configuration management can be configured in XML, and there is the concept of namespace in XML. In fact, it's a bit like the meaning of tags. After you give a namespace, the tags in that namespace context can be used in this XML file. To simplify configuration, you can see the difference between Spring AOP configuration with namespace and configuration without namespace.

Use Spring's namespace p assembly properties

Assembling values and references for Bean attributes using < property > elements is not too complicated. Nevertheless, Spring's namespace p provides another way to assemble Bean attributes, which does not need to be configured with so many angle brackets.

The schema URI of namespace p is http://www.springframework.org/schema/p . If you want to use namespace p, you only need to add the following declaration in the XML configuration of Spring:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

With this declaration, we can now use p: as the prefix of all attributes of the < Bean > element to assemble the attributes of the Bean. For demonstration, we redeclared the configuration of kenny Bean:

<bean id="kenny" class="com.springinaction.springidol.Instrumentalist" 
p:song = "Jingle Bells" 
p:instrument-ref = "saxophone" /> 

p: The value of the property "Jingsong" is set to the value of the property "Jingsong". Similarly, if the value of the P: instrument ref attribute is set to "Saxophone", the instrument attribute will be assembled using a Bean reference with ID saxophone- The ref suffix serves as an identifier to tell Spring that it should assemble a reference instead of a literal.

Whether you choose < property > or namespace p depends on you. They are equivalent. The main advantage of namespace p is its simplicity. When writing samples on fixed width paper, it is relatively more appropriate to choose a namespace. Therefore, in this book, you may see that I use namespace p from time to time, especially when the horizontal page space is compact.

In the Spring configuration file, there is a configuration file header:

<beans xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

This shows that in the current configuration file, the beans namespace is used, and < bean id = "" > can be used directly.

If you want to use the annotation driven element under the mvc namespace in this configuration file, it should be written as < mvc: annotation driven / >. Of course, you also need to tell the xml parser where the mvc namespace is defined, so that the parser can verify whether the mvc: opening element in the current file conforms to the mvc namespace:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

In this way, the parser will refer to spring-mvc-3.0 when interpreting MVC: namespace XSD this file to check whether < MVC: annotation driven / > is qualified.

Keywords: Spring xml mvc

Added by mc2007 on Sat, 19 Feb 2022 21:31:42 +0200