Spring namespace p Assembly attributes

Source: Internet
Author: User

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.

 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.