package com.h3.common; public class customer { private string name; private string address; private int age; public customer(string name, string address, int age) { this.name = name; this.address = address; this.age = age; } public customer(string name, int age, string address) { this.name = name; this.age = age; this.address = address; } //getter and setter methods public string tostring(){ return " name : " +name + "\n address : " + address + "\n age : " + age; } }
<!--spring-customer.xml--> <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-2.5.xsd"> <bean id="customerbean" class="com.h3.common.customer"> <constructor-arg> <value>h3</value> </constructor-arg> <constructor-arg> <value>188</value> </constructor-arg> <constructor-arg> <value>28</value> </constructor-arg> </bean> </beans>
package com.h3.common; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class app { public static void main( string[] args ) { applicationcontext context = new classpathxmlapplicationcontext(new string[] {"spring-customer.xml"}); customer cust = (customer)context.getbean("customerbean"); system.out.println(cust); } }
输出结果
name : h3 address : 28 age : 188
constructor arguments specified but no matching constructor found in bean 'customerbean' (hint: specify index and/or type arguments for simple parameters to avoid type ambiguities)
<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-2.5.xsd"> <bean id="customerbean" class="com.h3.common.customer"> <constructor-arg type="java.lang.string"> <value>h3</value> </constructor-arg> <constructor-arg type="java.lang.string"> <value>188</value> </constructor-arg> <constructor-arg type="int"> <value>28</value> </constructor-arg> </bean> </beans>
name : h3 address : 188 age : 28