Spring namespace p Assembly attributes
Use It is not too complicated to configure values and references for attributes whose elements are bean. Even so, spring namespace p provides another way to assemble bean attributes.
The schema url of namespace p is: http://www.springframework.org/schema/p
Let's look at the example (or the role and user model ):
Public class Roles {private int id; private String roleName; private Users users; // omit the set get method // rewrite the toString method to facilitate the test @ Overridepublic String toString () {return "Roles [id =" + id + ", roleName =" + roleName + ", users =" + users + "]" ;}}
Public class Users {private int id; private String name; // omit the set get method @ Overridepublic String toString () {return "Users [id =" + id + ", name = "+ name +"] ";}}
Spring Configuration:
Run the 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 [id = 1, roleName = administrator, users = Users [id = 2, name = John]
Select Or the namespace p depends on you. They are equivalent. The main point of namespace p is more concise.