package com.h3.core;
public class customer {
private item item;
private string itemname;
}
package com.h3.core;
public class item {
private string name;
private int qty;
}
<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="itembean" class="com.h3.core.item">
<property name="name" value="itema" />
<property name="qty" value="10" />
</bean>
<bean id="customerbean" class="com.h3.core.customer">
<property name="item" value="#{itembean}" />
<property name="itemname" value="#{itembean.name}" />
</bean>
</beans>
package com.h3.core;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.component;
@component("customerbean")
public class customer {
@value("#{itembean}")
private item item;
@value("#{itembean.name}")
private string itemname;
//...
}
package com.h3.core;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.component;
@component("itembean")
public class item {
@value("itema") //inject string directly
private string name;
@value("10") //inject interger directly
private int qty;
public string getname() {
return name;
}
//...
}
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.h3.core" /> </beans>
package com.h3.core;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
public class app {
public static void main(string[] args) {
applicationcontext context = new classpathxmlapplicationcontext("applicationcontext.xml");
customer obj = (customer) context.getbean("customerbean");
system.out.println(obj);
}
}
输出结果
customer [item=item [name=itema, qty=10], itemname=itema]