package com.zyiz.netmon; 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.zyiz.netmon.Customer"> <constructor-arg> <value>zyiz</value> </constructor-arg> <constructor-arg> <value>188</value> </constructor-arg> <constructor-arg> <value>28</value> </constructor-arg> </bean> </beans>
package com.zyiz.netmon; 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 : zyiz 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.zyiz.netmon.Customer"> <constructor-arg type="java.lang.String"> <value>zyiz</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 : zyiz address : 188 age : 28