[Spring practice series] (8) setter injection in Spring injection mode

Source: Internet
Author: User

[Spring practice series] (8) setter injection in Spring injection mode

Generally, the attributes of JavaBean are private and have a set of accessors. They exist in the form of setXXX () and getXXX. Spring can use the set method of the attribute to configure the attribute value for setter injection.

1. Inject simple valuesIn Spring, we can use Element to configure Bean attributes. In many ways Similarly, only values are injected by constructing parameters, and values are injected by calling the setter method of properties. For example, let's use setter injection to give students some basic information. setter injection must use the setXXX method to configure the attribute value. The Student class has two attributes: name and age. We provide the corresponding set method.
package com.sjf.bean;
/** * Student Entity * @author sjf0115 * */ public class Student { private String name; private int age; public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } @Override public String toString() { StringBuilder stringBuilder = new StringBuilder(); StringBuilder. append ("personal details:" + "\ n "); stringBuilder.append("name:" + name + "\n"); stringBuilder.append("age:" + age); return stringBuilder.toString(); } }Let's use setter injection to give students some basic information. The following XML shows Bean Configuration:

  
Once Student is instantiated, Spring will call The setter method of the attribute specified by the element injects a value into the attribute. In this XML code, The element instructs Spring to call the setName () method to set the value of the name attribute to "yoona" and call the setAge () method to set the value of the age attribute to 24. Note that xxx in name = "xxx" must be the same as XXX in setXXX. The element has no limit but to inject values of the String type. The value attribute can also specify values of the numeric type (int, float, java. lang. Double, etc.) and boolen type. Pay attention to the use of the value Attribute. Setting the value of the numeric type is not different from setting the value of the String type. Spring automatically determines the correct type of value based on the Bean attribute type. Because the age attribute of Bean is of the int type, Spring will automatically convert the string "24" to the int type before calling the setAge () method. 2. Reference other beansReference other beans in setter injection is basically the same as the constructor injection references other beans, which must be implemented using the ref attribute. Set a School class for Student:
public class School {
private String name; public void setName(String name) { this.name = name; } } 
private String name;
private int age; private School school; public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setSchool(School school) { this.school = school; }Before using it, we must declare it as a Bean in Spring and assign a name to the school:

  
After school is declared, you can assign it to Student now. Use setter injection to set the value for the school attribute:

  
  3. Inject internal beansWe have seen that yoona has been admitted to xidian University. Not only can yoona be admitted to the university, but other students can also go to the university through their own efforts. So students can share a School ). In fact, Bean sharing with other beans is very common in applications.

  
However, when we talk about personal interests, we cannot share them with others. Everyone has their own interests. We will use a very useful Spring technology: internal Bean (inner bean ). Internal beans are beans defined in other beans.
package com.sjf.bean;
public class Hobby { private String desc; public void setDesc(String desc) { this.desc = desc; } @Override public String toString() { return desc; } } 
package com.sjf.bean;
/** * Student Entity * @author sjf0115 * */ public class Student { private String name; private int age; private Hobby hobby; public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setHobby(Hobby hobby) { this.hobby = hobby; } @Override public String toString() { StringBuilder stringBuilder = new StringBuilder(); StringBuilder. append ("personal details:" + "\ n "); stringBuilder.append("name:" + name + "\n"); stringBuilder.append("age:" + age + "\n"); stringBuilder.append("hobby:" + hobby.toString()); return stringBuilder.toString(); } }Perform the following configuration to declare holobby as an internal Bean:
As you can see, an internal Bean directly declares Element The child node of the element. Internal beans are not limited to setter injection. We can also assemble internal beans into the input parameters of the constructor. Note that the internal Bean does not have the ID attribute. Although configuring an ID attribute for an internal Bean is completely legal, it is not necessary because we will never reference the internal Bean by name. This also highlights the biggest disadvantage of using internal beans: they cannot be reused. Internal Beans only apply to one injection and cannot be referenced by other beans. Reference: Spring practice

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.