Spring uses propertypathfactorybean for property value Injection

Source: Internet
Author: User

This article from: http://www.blogjava.net/wangxinsh55/archive/2010/08/10/328427.html

In actual application, the attribute of an instance may be an attribute of another object. Spring supports directly assigning the attribute value of the bean instance to a variable.

The injection of attribute values is completed through propertypathfactorybean. propertypathfactorybean is used to obtain the attributes of the target bean. The obtained values can be injected into other beans or defined as new beans.

Entity class:

public class Person {    private Son son;    private String age;        public Son getSon() {        return son;    }        public void setSon(Son son) {        this.son = son;    }        public String getAge() {        return age;    }        public void setAge(String age) {        this.age = age;    }}
public class Son {    private String age;    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }}

Configuration File: provides four types of Injection

<? XML version = "1.0" encoding = "gb2312"?> <Beans default-autowire = "byname" xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: util = "http://www.springframework.org/schema/util" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/util http: // localhost: 8080/Schema/www. spri Ngframework.org/schema/util/spring-util-2.0.xsd "> <bean id =" person "class =" com. alibaba. chj. factorybean. person "Scope =" prototype "> <property name =" Age "> <value> 30 </value> </property> <property name =" son "> <Bean class = "com. alibaba. chj. factorybean. son "> <property name =" Age "> <value> 16 </value> </property> </bean> <! -- The following will pass the son attribute age of the person property to the age attribute of the son1 instance --> <bean id = "son1" class = "com. alibaba. chj. factorybean. son "> <property name =" Age "> <! -- The following is a simple way to access the bean attribute. In this way, you can assign the age attribute of the bean "person" to the age attribute of the bean "son1" --> <bean id = "person. son. age "class =" org. springframework. beans. factory. config. propertypathfactorybean "/> </property> </bean> <! -- The following will obtain the result son, which will be the son value of the person Bean --> <bean id = "son2" class = "org. springframework. beans. factory. config. propertypathfactorybean "> <property name =" targetbeanname "> <value> person </value> </property> <property name =" propertypath "> <value> son </value> </property> </bean> <! -- The following code returns result 16, which is the age attribute of son of person Bean --> <bean id = "son3" class = "org. springframework. beans. factory. config. propertypathfactorybean "> <property name =" targetbeanname "> <value> person </value> </property> <property name =" propertypath "> <value> son. age </value> </property> </bean> <! -- The following result is 30, which is the age attribute of the internal bean of the bean --> <bean id = "son4" class = "org. springframework. beans. factory. config. propertypathfactorybean "> <property name =" targetobject "> <Bean class =" com. alibaba. chj. factorybean. person "> <property name =" Age "> <value> 30 </value> </property> </bean> </property> <property name =" propertypath "> <value> age </value> </property> </bean> </beans>

Test code:

public class PropertyPathTest {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext(                                                                        "com/alibaba/chj/factorybean/propertyPathFactoryBean.xml");        Son son1 = (Son) context.getBean("son1");        Son son2 = (Son) context.getBean("son2");        System.out.println("person age is:" + son1.getAge());        System.out.println("person age is:" + son2.getAge());        System.out.println(context.getBean("son3"));        System.out.println(context.getBean("son4"));    }}

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.