测试类
public class Construct { private String address; private long phone; public Construct(String name, int age) { System.out.println("> " + name + ", " + age); } public String getAddress() { return address; } public void setAddress(String address) { System.out.println("> " + address); this.address = address; } public long getPhone() { return phone; } public void setPhone(long phone) { System.out.println("> " + phone); this.phone = phone; } }
<!-- 使用name --> <bean id="construct_c1" class="cn.ycx.bean.Construct"> <constructor-arg name="age" value="1" type="int"></constructor-arg> <!-- type可以省略 --> <constructor-arg name="name" value="Tom_1"></constructor-arg> <!-- 属性配置 --> <property name="address" value="xi‘an"></property> <property name="phone" value="18000"></property> </bean> <!-- 使用index --> <bean id="construct_c2" class="cn.ycx.bean.Construct"> <constructor-arg index="1" value="2" type="int"></constructor-arg> <!-- type可以省略 --> <constructor-arg index="0" value="Tom_2"></constructor-arg> </bean> <!-- 省略name和index,值顺序必须和方法定义保持一致 --> <bean id="construct_c3" class="cn.ycx.bean.Construct"> <constructor-arg value="Tom_3" type="String"></constructor-arg> <constructor-arg value="3" type="int"></constructor-arg> </bean> <!-- c命名空间name --> <bean id="construct_c4" class="cn.ycx.bean.Construct" c:name="Tom_4" c:age="4" p:address="beijing" p:phone="136000"></bean> <!-- c命名空间index --> <bean id="construct_c5" class="cn.ycx.bean.Construct" c:_0="Tom_5" c:_1="5"></bean>
原文地址:https://www.cnblogs.com/yangchongxing/p/9872128.html
时间: 2024-10-12 15:42:12