public class customer { @value("#{addressbean.country}") private string country;
package com.h3.core; import org.springframework.beans.factory.annotation.value; import org.springframework.stereotype.component; @component("customerbean") public class customer { @value("#{addressbean}") private address address; @value("#{addressbean.country}") private string country; @value("#{addressbean.getfulladdress('h3')}") private string fulladdress; //getter and setter methods @override public string tostring() { return "customer [address=" + address + "\n, country=" + country + "\n, fulladdress=" + fulladdress + "]"; } }
package com.h3.core; import org.springframework.beans.factory.annotation.value; import org.springframework.stereotype.component; @component("addressbean") public class address { @value("gaodeng, qiongshang") private string street; @value("571100") private int postcode; @value("cn") private string country; public string getfulladdress(string prefix) { return prefix + " : " + street + " " + postcode + " " + country; } //getter and setter methods public void setcountry(string country) { this.country = country; } @override public string tostring() { return "address [street=" + street + ", postcode=" + postcode + ", country=" + country + "]"; } }
执行结果
customer obj = (customer) context.getbean("customerbean"); system.out.println(obj);
输出结果
customer [address=address [street=gaodeng, qiongshang, postcode=571100, country=cn] , country=cn , fulladdress=h3 : gaodeng, qiongshang 571100 cn]
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="customerbean" class="com.h3.core.customer"> <property name="address" value="#{addressbean}" /> <property name="country" value="#{addressbean.country}" /> <property name="fulladdress" value="#{addressbean.getfulladdress('h3')}" /> </bean> <bean id="addressbean" class="com.h3.core.address"> <property name="street" value="gaodeng, qiongshang" /> <property name="postcode" value="571100" /> <property name="country" value="cn" /> </bean> </beans>