A, C namespaceWhen you inject beans through a constructor, you usually need to use the <constructor-arg> element as an alternative to the C-namespace of spring. The C-namespace is added in Spring 3.0, which is a more concise way to describe constructor parameters in XML. To use the C-namespace, you must declare its schema at the top of the XML, as follows:
<?xml version= "1.0" encoding= "UTF-8"?> <beans
xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns= "Http://www.springframework.org/schema/beans"
xmlns:c= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/C"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/ Beans/spring-beans.xsd ">
</beans>
After the C-namespace and schema declarations, you can use it to declare the constructor arguments as follows:
<bean id= "Compactdisc" class= "Cn.javacodes.spring.beans.soundsystem.Transfer" ></bean>
<bean ID = "CDPlayer" class= "Cn.javacodes.spring.beans.soundsystem.CDPlayer"
c:cd-ref= "Compactdisc"/>
You can see that there are some problems in the way above, the CD in C:cd-ref is the name of the constructor parameter, which is very unfriendly to the refactoring of the late code, so in order to solve this problem, you can use the index of the parameter instead of the parameter name, as follows:
<bean id= "Compactdisc" class= "Cn.javacodes.spring.beans.soundsystem.Transfer" ></bean>
<bean ID = "CDPlayer" class= "Cn.javacodes.spring.beans.soundsystem.CDPlayer"
c:_0-ref= "Compactdisc"/>
Of course, if the constructor has only one parameter, you can omit the index together:
<bean id= "Compactdisc" class= "Cn.javacodes.spring.beans.soundsystem.Transfer" ></bean>
<bean ID = "CDPlayer" class= "Cn.javacodes.spring.beans.soundsystem.CDPlayer"
c:_-ref= "Compactdisc"/>
However, in my test, this method will be an error in idea.
Second, assembly set
(i) assemble list and setThe code for the Blankdisc class is as follows:
Package cn.javacodes.spring.beans.soundsystem.collections;
Import Cn.javacodes.spring.beans.soundsystem.CompactDisc;
Import java.util.List;
/**
* Created by Eric on 2016/10/16.
*/Public
class Blankdisc implements Compactdisc {
private String title;
Private String artist;
private list<string> tracks;
Public Blankdisc (string title, string artist, list<string> tracks) {
this.title = title;
this.artist = artist;
This.tracks = tracks;
}
public void Play () {
System.out.println ("playing" + Artist + "album:" + title);
for (String track:tracks) {
System.out.println ("-track:" + track);}}
}
A list is required to configure the Bean for this class in spring, and you can, of course, pass it to a null value:
<bean id= "Compactdisc" class= "Cn.javacodes.spring.beans.soundsystem.collections.BlankDisc" >
< Constructor-arg value= "Transfer" ></constructor-arg>
<constructor-arg value= "Zhou/Xiao Gang" ></ constructor-arg>
<constructor-arg><null/></constructor-arg>
</bean>
However, if NULL is passed, when the play () method is called, it is bound to produce a nullpointerexception exception, and usually we will assemble a list for it. We have a lot of solutions, first of all, you can declare it as a list using the <list> element:
<bean id= "Compactdisc" class= " Cn.javacodes.spring.beans.soundsystem.collections.BlankDisc "> <constructor-arg value=" Transfer "></
constructor-arg> <constructor-arg value= "Zhou/Xiao Gang" ></constructor-arg> <constructor-arg>
<list> <value> Forget </value> <value> sell </value> <value> Lonely bombing </value> <value> game love </value> <value> Heart
Knots </value> <value> notepad </value> <value> beer bubbles </value> <value> Twilight </value> <value> last bus </value> <value> whim </va Lue> <value> time </value> </list> </constructor-arg> </ Bean>
Of course, you can also use <ref> to replace <value> to refer to other beans for the purpose, again do not repeat. In our example, the constructor parameter type is list, so it is reasonable to inject with the <list> element, however, we can still use the <set> element to inject:
<bean id= "Compactdisc" class= " Cn.javacodes.spring.beans.soundsystem.collections.BlankDisc "> <constructor-arg value=" Transfer "></
constructor-arg> <constructor-arg value= "Zhou/Xiao Gang" ></constructor-arg> <constructor-arg>
<set> <value> Forget </value> <value> sell </value> <value> Lonely bombing </value> <value> game love </value> <value> Heart knot
</value> <value> notepad </value> <value> beer bubbles </value> <value> Twilight </value> <value> last bus </value> <value> whim </val Ue> <value> time </value> </set> </constructor-arg> </be An>
The difference between <set> and <list> elements is not significant, and the most important difference is that when spring creates a collection to assemble, it creates a set or a list. If it is set, all duplicate values are ignored, and the order of the deposits is not guaranteed, which is believed to have a clear understanding of the difference between set and list. In either case, however,,<set> and <list> can be used to assemble lists, sets, and even arrays.
(b), assembly mapWe will change the above blankdisc slightly to change the original list to map:
Private String title;
Private String artist;
private map<string,string> tracks;
Public Blankdisc (string title, string artist, map<string, string> tracks) {
this.title = title;
this.artist = artist;
This.tracks = tracks;
}
The way to assemble a map is as follows:
<bean id= "Compactdisc" class= "Cn.javacodes.spring.beans.soundsystem.collections.BlankDisc" >
< Constructor-arg value= "Transfer" ></constructor-arg>
<constructor-arg value= "Zhou/Xiao Gang" ></ constructor-arg>
<constructor-arg>
<map>
<entry key= "Forget" value= "5:12"/>
<entry key= "Selling" value= "4:54"/>
<entry
... </map>
</constructor-arg>
</bean>
Of course <key> and <value> elements can be substituted with <key-ref> and <value-ref> elements respectively for referencing other beans.
(iii), assembly propertiesThe properties are very similar to map, but the difference is that the key and value of the map can be any type of object, and properties requires both key and value to be of type string. In many scenarios our key and value are indeed string types, such as the configuration of database connection information. To demonstrate the configuration of properties, we create a class datasource:
Package cn.javacodes.spring.beans.properties;
Import java.util.Properties;
/**
* Created by Eric on 2016/10/16.
*/Public
class DataSource {
private properties properties;
Public DataSource (Properties properties) {
this.properties = properties;
}
Public Properties getProperties () {
return Properties;
}
public void SetProperties (properties properties) {
this.properties = properties;
}
}
The spring XML configuration is as follows:
<bean id= "DataSource" class= "Cn.javacodes.spring.beans.properties.DataSource" >
<constructor-arg>
<props>
<prop key= "user" >root</prop>
<prop key= "password" >123456</prop >
<prop key= "url" >jdbc:mysql:///test</prop>
<prop key= "Driver" > com.mysql.jdbc.driver</prop>
</props>
</constructor-arg>
</bean>
Three, p namespacesWe know that the <property> element is required to configure the properties of the bean, and in order to simplify this approach, like the C namespace, Spring provides us with the P namespace. In order to use the P-namespace, it must be declared at the top of the XML:
<?xml version= "1.0" encoding= "UTF-8"?> <beans
xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns= "Http://www.springframework.org/schema/beans"
xmlns:p= "http://www.springframework.org/schema/p"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/ Beans/spring-beans.xsd "> ...
</beans>
You can then use the P-namespace, using the following method:
<bean id= "CDPlayer" class= "Cn.javacodes.spring.beans.soundsystem.CDPlayer" p:cd-ref=
"Compactdisc"/>
The name of the property ends with-ref, which represents the bean reference instead of the literal value (String) that spring is assembling.
Iv. util namespacesBoth the P-namespace and the C-namespace cannot assemble the collection type, but we can use the Util namespace to simplify assembly of the collection, declared as follows:
<?xml version= "1.0" encoding= "UTF-8"?> <beans
xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns= "Http://www.springframework.org/schema/beans"
xmlns:p= "http://www.springframework.org/schema/p"
xmlns:c= "http://www.springframework.org/schema/c"
xmlns:util= "http://www.springframework.org/schema/ Util "
xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/schema /beans/spring-beans.xsd ">
</beans>
You can now use the Util namespace, for example:
<util:list id= "Tracklist" >
<value> Forget </value>
<value> sell </value>
< Value> Lonely bombing </value>
<value> game love </value>
<value> heart knot </value>
< Value> Notepad </value>
<value> beer bubbles </value>
<value> Dusk </value>
<value > Last bus </value>
<value> whim </value>
<value> time </value>
</util: List>
You can then easily reference the list as you would reference other beans, as follows:
<bean id= "Compactdisc" class= "Cn.javacodes.spring.beans.soundsystem.collections.BlankDisc"
c:title= " Transfer "c:artist=" Zhou/Xiao Gang "c:tracks-ref=" tracklist "/>
<util:list> is just one of the many elements provided by the util-namespace, and the following table lists all the elements provided by the util-namespace:
Elements |
Describe |
<util:constant> |
Reference a public static field of a type with a wall paint exposed as a bean |
<util:list> |
Create a bean of type java.util.List that contains a value or reference |
<util:map> |
Create a bean of type JAVA.UTIL.MAP that contains a value or reference |
<util:properties> |
Create a bean of type java.util.Properties |
<util:property-path> |
Reference a Bean's attribute (or inline property) and expose it as a bean |
<util:set> |
Create a bean of type java.util.set that contains a value or reference |
This article for Bo Master Independent blog (https://javacodes.cn) synchronous publication, reproduced please indicate the source.
View Original: https://javacodes.cn/333.html