Spring Assembly set
We have learned how to use simple attributes (using the value Attribute) of spring equipment and reference attributes of other beans (using the ref attribute ). However, value and ref are only useful when the Bean property value is a single value. When the bean property value is a plural value ----- if the property type is set.
Spring provides four collection configuration elements When configuring bean properties of the collection type, as shown below.
| Set Element |
Purpose |
| |
Value of the list type assembly. Repeated values are allowed. |
| |
Assembly set type, repeated is not allowed |
|
Value of the Assembly map type. The name and value can be of any type. |
| |
Value of the Assembly properties type. The name and value must be of the String type. |
I. Assemble List, Set, and Array
Next let's take a look
Public class Roles {private String roleName; private List
Users; // omit the set get method // rewrite the toString method to facilitate testing @ Overridepublic String toString () {return "Roles [roleName =" + roleName + ", users = "+ users +"] ";}
Public class Users {private String name; // omit the set get method @ Overridepublic String toString () {return "Users [name =" + name + "]" ;}}Spring Configuration:
Can assemble List, Set, Array (String type)
Run the test main method:
public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");Roles r=(Roles)context.getBean("roles");System.out.println(r.toString());}Output result:
Roles [roleName = counselor, users = [Users [name = Zhang San], Users [name = Li Si], Users [name = Li Si]
Element to assemble the collection type or array type:
Output result:
Roles [roleName = counselor, users = [Users [name = Zhang San], Users [name = Li Si]
Ii. Assembly Set:
Public class Roles {private String roleName; private Map
Users; // omit the set get method // rewrite the toString method to facilitate testing @ Overridepublic String toString () {return "Roles [roleName =" + roleName + ", users = "+ users +"] ";}
Run the main method of the test and output the following results:
Roles [roleName = counselor, users = {USERS1 = Users [name = Zhang San], USERS2 = Users [name = Li Si]}]
In An element consists of one key and one value. The key and value can be of a simple type or can be referenced by other beans. These attributes will help us specify Key and value
| Attribute |
Purpose |
| Key |
Specify the entry key in map as String |
| Key-ref |
Specify the entry key in map as the reference of other beans in the String context. |
| Value |
Specify the entry key in map as String |
| Value-ref |
Specify the entry key in map as the reference of other beans in the String context. |
Iii. Assembly Properties set
Public class Roles {private String roleName; private Properties users; // The set get method is omitted. // The toString method is rewritten for testing. @ Overridepublic String toString () {return "Roles [roleName =" + roleName + ", users =" + users + "]" ;}}Spring Configuration:
Zhang San
19
Output result:
Roles [roleName = counselor, users = {AGE = 19, USERS = James}]
Iv. null Assembly values
In addition to assembling any type of values for bean attributes or constructor parameters, Spring can also assemble a null value. Or, more accurately, Spring can assemble null values.
Public class Roles {private String roleName; private Users users; // The set get method is omitted. // The toString method is rewritten for testing. @ Overridepublic String toString () {return "Roles [roleName =" + roleName + ", users =" + users + "]" ;}}
Spring Configuration:
Output result:
Roles [roleName = counselor, users = null]