For injection of a simple data type (byte, char, short, int, float, double, long) or String, you only need to write the label <value>. For example:
[Html]
<Property name = "propertyName" value = "simpleValue"/>
<Property name = "propertyName" value = "simpleValue"/> or
[Html]
<Property name = "propertyName">
<Value> simpleValue </value>
</Property>
<Property name = "propertyName">
<Value> simpleValue </value>
</Property>
Or p Mode
If you want to inject a set (list, set, map, pros), what should you do?
If the generic type of the set is a simple data type, such as List <String>, you can do this:
[Html]
<Property name = "students">
<List>
<Value> student1 </value>
<Value> student2 </value>
...
</List>
</Property>
<Property name = "students">
<List>
<Value> student1 </value>
<Value> student2 </value>
...
</List>
</Property>
If the generic type of the set is a reference type, such as List <Student>, you can use the internal bean to implement
[Java]
<Property name = "students">
<List>
<Bean class = "FullQualifiedNameClass">
<Property name = "name" value = "zhangsan"/>
<Property name = "age" value = "20"/>
</Bean>
<Bean class = "FullQualifiedNameClass">
<Property name = "name" value = "lisi"/>
<Property name = "age" value = "22"/>
</Bean>
......
</List>
</Property>
<Property name = "students">
<List>
<Bean class = "FullQualifiedNameClass">
<Property name = "name" value = "zhangsan"/>
<Property name = "age" value = "20"/>
</Bean>
<Bean class = "FullQualifiedNameClass">
<Property name = "name" value = "lisi"/>
<Property name = "age" value = "22"/>
</Bean>
......
</List>
</Property>
The following is an example:
Province:
[Java]
Public class Province {
Private String name; // province name
Private long population; // population
Private List <String> cities; // city List
Private List <Official> officials; // Officer
// Standard setter, getter, and toString methods are omitted.
}
Public class Province {
Private String name; // province name
Private long population; // population
Private List <String> cities; // city List
Private List <Official> officials; // Officer
// Standard setter, getter, and toString methods are omitted.
}
[Java]
Public class Official {
Private String name;
Private String title;
Private String age;
// The setter, getter, and toString methods are omitted.
}
Public class Official {
Private String name;
Private String title;
Private String age;
// The setter, getter, and toString methods are omitted.
}
Bean configuration file:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
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.xsd">
<Bean id = "sichuan" class = "org. spring. collection. Province" scope = "singleton">
<Property name = "name">
<Value> Sichuan </value>
</Property>
<Property name = "population" value = "87225224"/>
<Property name = "cities">
<List>
<Value> Chengdu </value>
<Value> Mianyang </value>
<Value> Deyang </value>
<Value> Panzhihua </value>
<Value> Suining </value>
<Value> Jiangyou </value>
</List>
</Property>
<Property name = "officials">
<List>
<Bean class = "org. spring. collection. Official">
<Property name = "name" value = "zhangsan"/>
<Property name = "age" value = "45"/>
<Property name = "title" value = "mayor"/>
</Bean>
<Bean class = "org. spring. collection. Official">
<Property name = "name" value = "lisi"/>
<Property name = "age" value = "55"/>
<Property name = "title" value = "coutrier"/>
</Bean>
<Bean class = "org. spring. collection. Official">
<Property name = "name" value = "wangwu"/>
<Property name = "age" value = "42"/>
<Property name = "title" value = "villager"/>
</Bean>
</List>
</Property>
</Bean>
</Beans>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
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.xsd">
<Bean id = "sichuan" class = "org. spring. collection. Province" scope = "singleton">
<Property name = "name">
<Value> Sichuan </value>
</Property>
<Property name = "population" value = "87225224"/>
<Property name = "cities">
<List>
<Value> Chengdu </value>
<Value> Mianyang </value>
<Value> Deyang </value>
<Value> Panzhihua </value>
<Value> Suining </value>
<Value> Jiangyou </value>
</List>
</Property>
<Property name = "officials">
<List>
<Bean class = "org. spring. collection. Official">
<Property name = "name" value = "zhangsan"/>
<Property name = "age" value = "45"/>
<Property name = "title" value = "mayor"/>
</Bean>
<Bean class = "org. spring. collection. Official">
<Property name = "name" value = "lisi"/>
<Property name = "age" value = "55"/>
<Property name = "title" value = "coutrier"/>
</Bean>
<Bean class = "org. spring. collection. Official">
<Property name = "name" value = "wangwu"/>
<Property name = "age" value = "42"/>
<Property name = "title" value = "villager"/>
</Bean>
</List>
</Property>
</Bean>
</Beans>
Run it:
[Java]
Public class App {
Public static void main (String [] args ){
ApplicationContext context = new ClassPathXmlApplicationContext (new String [] {"SpringConfig. xml "});
Province siChuan = (Province) context. getBean ("sichuan ");
System. out. println (siChuan );
}
}
Public class App {
Public static void main (String [] args ){
ApplicationContext context = new ClassPathXmlApplicationContext (new String [] {"SpringConfig. xml "});
Province siChuan = (Province) context. getBean ("sichuan ");
System. out. println (siChuan );
}
}
Print Output:
[Plain]
Province [name = Sichuan, population = 87225224, cities = [Chengdu, Mianyang, Deyang, Panzhihua, Suining, Jiangyou], officials = [Official [name = zhangsan, title = mayor, age = 45], Official [name = lisi, title = coutrier, age = 55], Official [name = wangwu, title = villager, age = 42]
Province [name = Sichuan, population = 87225224, cities = [Chengdu, Mianyang, Deyang, Panzhihua, Suining, Jiangyou], officials = [Official [name = zhangsan, title = mayor, age = 45], Official [name = lisi, title = coutrier, age = 55], Official [name = wangwu, title = villager, age = 42]
Here we only talk about the List Set, Set, Map, and Properties.