How to inject values into spring bean properties

Source: Internet
Author: User

In spring, there are three ways to inject values into bean properties.
    • The normal way
    • Shortcut
    • "P" mode
See a simple Java class that contains two properties-name and type. You will use spring to inject values into this bean property later.
Package Com.yiibai.common;public class Filenamegenerator {private string Name;private string Type;public string GetName ( {return name;} public void SetName (String name) {this.name = name;} Public String GetType () {return type;} public void SetType (String type) {this.type = type;}}
1. The normal method injects a value into a "value" tag and ends with a "property" tag.
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" http://www.springframework.org/schema/beanshttp:// Www.springframework.org/schema/beans/spring-beans-2.5.xsd "><bean id=" Filenamegenerator "class=" Com.yiibai.common.FileNameGenerator "><property name=" name "><value>yiibai</value></ Property><property name= "Type" ><value>txt</value></property></bean></beans >
2, the shortcut injects the value of the "Value" property.
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" http://www.springframework.org/schema/beanshttp:// Www.springframework.org/schema/beans/spring-beans-2.5.xsd "><bean id=" Filenamegenerator "class=" Com.yiibai.common.FileNameGenerator "><property name=" name "value=" Yiibai "/><property name=" type "value = "TXT"/></bean></beans>
3. The "P" mode is injected into a property by using "P" mode.
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:p=" http://www.springframework.org/schema/p "xsi:schemalocation="/HTTP/ Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd ">< Bean id= "Filenamegenerator" class= "Com.yiibai.common.FileNameGenerator"              p:name= "Yiibai" p:type= "TXT"/>< /beans>
Remember to declare xmlns:p= "http://www.springframework.org/schema/p" in the Spring XML bean configuration file. Summarizing the use of these methods is entirely based on personal preference and does not affect the value of the injected Bean property. Http://www.yiibai.com/spring/how-to-define-bean-properties-in-spring.html

How to inject values into spring bean properties

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.