spring设置构造,name,ref,map,set,list赋值
、先建立一个实例类Person
public class Person {private int id;private String name;private int age;private Person friend;private List<String> list;private Set<String> set;private Map<String, String> map;public Person(){}public Person(int id,String name){this.id=id;this.name=name;}//实现get set 方法,这里我就不显示给大家了实例spring xml中的bean
<bean class="spring.bean.Person" id="p1" name="a,b;/login"><constructor-arg ><value>1</value></constructor-arg><constructor-arg type="java.lang.String" value="mou"></constructor-arg><property name="age" value="12" ></property><property name="friend" ><!-- ref中bean与id相同 --><ref bean="p1"/><!-- new一个新的,在这里id、name不在起作用 --><!-- <bean class="spring.bean.Person" ></bean> --></property><property name="list" ><list><!-- 基本数据类型用value,对象用bean --><value>mou</value><value>yun</value><value>fei</value></list></property><property name="set" ><set><!-- 基本数据类型用value,对象用bean --><value>mou</value><value>yun</value><value>fei</value></set></property><property name="map" ><map key-type="java.lang.String"><entry><key><value>1</value></key><value>mou</value></entry><entry ><key><value>2</value></key><value>yun</value></entry><entry><key><value>3</value></key><value>fei</value></entry></map><!-- 或者 <props ><prop key="1">mou</prop><prop key="2">yun</prop><prop key="3">fei</prop></props> --></property></bean>
1、<<ref bean="p1"/>
ref必须是本xml中已经存在的bean,并且必须使用的是id
<bean class="spring.bean.Person" ></bean>
是new一个新的,在这里id、name不在起作用
2、name="a,b;/login"
则是在使用时
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");Person p = (Person) app.getBean("/login");getBean("a")可以,getBean("b")可以,getBean("/login");也可以
3、构造方法在spring的xml中赋值
在spring的xml中赋值有参构造有两种方法,一个是标签 ,一个是属性,下面这个就是对public Person(int id,String name)构造的赋值,有的人说如果还有 public Person(String name)构造呢?那就再重新写一个bean,写一个<constructor-arg >就可以了
<constructor-arg > <value>1</value></constructor-arg><constructor-arg type="java.lang.String" value="mou"></constructor-arg>
4、实体类中private List<String> list;,在spring的xml中赋值,set的赋值基本上与list一致,map的赋值则可以使用<map>或者<props>
<property name="list" ><list><!-- 基本数据类型用value,对象用bean --><value>mou</value><value>yun</value><value>fei</value></list></property>
三、测试
public void test(){//多个xml用,号分隔做参数ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");Person p = (Person) app.getBean("/login");System.out.println(p.getId());System.out.println(p.getName());System.out.println(p.getAge());//对象System.out.println("---------------对象--------");System.out.println(p.getFriend().getName());//listSystem.out.println("---------------list--------");List<String> list =p.getList();for (int i = 0; i < list.size(); i++) {System.out.println(list.get(i));}//setSystem.out.println("---------------set--------");Set<String> set =p.getSet();for(String s : set){System.out.println(s);}//mapSystem.out.println("---------------map--------");Map<String, String> map = p.getMap();for(Entry<String, String> entry: map.entrySet()){System.out.println(entry.getKey()+"--------"+entry.getValue());}}}