Assigning values from a bean in a spring configuration file

Source: Internet
Author: User

Basic data type assignment-through the property in the bean in the spring configuration file

Extension-In this way you can assign a value to a property that is configured to connect data

1, if it is a basic data type, you can set the initial value of the property in the object by setter method, apply: You can write something that was previously written in DBC.

2. If the type of the attribute is not a base type or String, you can assign a value to the object using a reference (ref in the property in The bean)

Extension-In this way, the connection value of the database can be assigned to the implementation class

3, set the value of the property, note to initialize the collection. The basic data type is not initialized because it is initialized by default (infrequently used)

4, by the construction method for the property assignment (not commonly used, usually using Setter method Assignment)

5. Automatic binding of attributes (not commonly used)

(1)byname

(2)Bytype

(3)AutoDetect, press byname First, then press bytype

Example:

1. Create a user class

[Java]View PlainCopyprint?
  1. Public class User {
  2. private String userid;
  3. private String username;
  4. private int age;
  5. public int getage () {
  6. return age;
  7. }
  8. public void Setage (int.) {
  9. this.age = age;
  10. }
  11. Public String GetUserid () {
  12. return userid;
  13. }
  14. public void Setuserid (String userid) {
  15. This.userid = userid;
  16. }
  17. Public String GetUserName () {
  18. return username;
  19. }
  20. public void Setusername (String username) {
  21. this.username = Username;
  22. }
  23. }

2. In the configuration file

[HTML]View PlainCopyprint?
  1. <Bean id="user" class="Org.liky.spring.pojo.User">
  2. <!--can initialize data for a property through the setter method of the property -
  3. < name="userid" value="abc"> </Property>
  4. </Bean>

3. Test procedures

[Java]View PlainCopyprint?
      1. ApplicationContext context = new Classpathxmlapplicationcontext (
      2. "Applicationcontext.xml");
      3. User user = (user) Context.getbean ("user");
      4. System.out.println (User.getuserid ());
      5. System.out.println (new User (). GetUserid ());

Assigning values from a bean in a spring configuration file

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.