在spring 刚开始学习的时候,就只有简单的注值,在beans.xml中进行配置,将各种类交给spring来管理。这里,有几种简单的beans.xml的配置方法。
有多种bean的配置方法,大体上分为三种,一种为通过构造函数来配置,一种通过属性来配置,还有一种是通过p空间来配置。一下来简单说说这三种配置方法。
配置之前,先来几个实体类,以下的配置都是基于这几个实体类。
package com.yc.spring02; public class Student { private String sid; private String name; private String sex; private Address address; @Override public String toString() { return "Student [sid=" + sid + ", name=" + name + ", sex=" + sex + ", address=" + address + "]"; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((sex == null) ? 0 : sex.hashCode()); result = prime * result + ((sid == null) ? 0 : sid.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (sex == null) { if (other.sex != null) return false; } else if (!sex.equals(other.sex)) return false; if (sid == null) { if (other.sid != null) return false; } else if (!sid.equals(other.sid)) return false; return true; } public Student(String sid, String name, String sex) { super(); this.sid = sid; this.name = name; this.sex = sex; } public Student() { super(); } public Student(String sid, String name, String sex, Address address) { super(); this.sid = sid; this.name = name; this.sex = sex; this.address = address; } public Student(Address address){ this.address = address; } }
Address类:
package com.yc.spring02; public class Address { private String country; private String city; @Override public String toString() { return "Address [country=" + country + ", city=" + city + "]"; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((city == null) ? 0 : city.hashCode()); result = prime * result + ((country == null) ? 0 : country.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Address other = (Address) obj; if (city == null) { if (other.city != null) return false; } else if (!city.equals(other.city)) return false; if (country == null) { if (other.country != null) return false; } else if (!country.equals(other.country)) return false; return true; } public Address(String country, String city) { super(); this.country = country; this.city = city; } public Address() { super(); } }
关系:Student类中有一个address属性,该属性的类型是Address
好了现在简单来讲讲bean的配置。
(一)通过构造函数来配置。
<bean id="student01" class="com.yc.spring02.Student"> <constructor-arg value="YC001">constructor-arg> <constructor-arg value="yc">constructor-arg> <constructor-arg value="男">constructor-arg> bean> <bean id="student02" class="com.yc.spring02.Student"> <constructor-arg name = "name" value="yc">constructor-arg> <constructor-arg name = "sex" value="男">constructor-arg> <constructor-arg name = "sid" value="YC001">constructor-arg> bean> <bean id="student03" class="com.yc.spring02.Student"> <constructor-arg index="1" value="yc">constructor-arg> <constructor-arg index="2" value="男">constructor-arg> <constructor-arg index="0" value="YC001">constructor-arg> bean>
注意:要想要构造方法配置,则在实体类中必须要有对应的构造方法。
(二)通过属性来配置
注意:通过属性来配置时,要有实体类中要有对应的set方法,如果没有,则配置失败
<bean id="student06" class="com.yc.spring02.Student"> <property name="sid" value="yc001">property> <property name="name" value="yc">property> <property name="sex" value="男">property>
<property name="address" ref="addr01">property>
bean>
来个小结:关于构造方法配置和属性配置两种,可以混用,同时,还有一些骚操作,如下:
<bean id="student07" class="com.yc.spring02.Student"> <constructor-arg index="1" value="yc">constructor-arg> <constructor-arg index="2" value="男">constructor-arg> <constructor-arg index="0" value="YC001">constructor-arg> <constructor-arg index="3"> <null/> constructor-arg> bean>
<bean id="addr01" class="com.yc.spring02.Address"> <property name="country" value="中国">property> <property name="city"> <value><衡阳>]]>value> property> bean> <bean id="student04" class="com.yc.spring02.Student"> <constructor-arg index="1" value="yc">constructor-arg> <constructor-arg index="2" value="男">constructor-arg> <constructor-arg index="0" value="YC001">constructor-arg> <property name="address" ref = "addr01">property> bean> <bean id="student05" class="com.yc.spring02.Student"> <property name="sid" value="yc001">property> <property name="name" value="yc">property> <property name="sex" value="男">property> <property name="address" > <bean class="com.yc.spring02.Address"> <property name="country" value="中国">property> <property name="city" value="长沙">property> bean> property> bean>
(三)p命名空间的使用,p命名空间可以是配置简化,要想使用p空间,则要添加p空间的声明:xmlns:p="http://www.springframework.org/schema/p" ,放在beans标签里面,
具体p空间的使用如下:
<bean id="student08" class="com.yc.spring02.Student" p:sid="yc002" p:name="yc" p:sex="男" p:address-ref="addr01">bean> <bean id="student09" class="com.yc.spring02.Student" p:sid="yc002" p:name="yc" p:sex="男" autowire="byName">bean> <bean id="address" class="com.yc.spring02.Address"> <property name="country" value="中国">property> <property name="city" value="常数">property> bean> <bean id="student12" class="com.yc.spring02.Student" p:sid="yc002" p:name="yc" p:sex="男" autowire="default">bean> <bean id="student13" class="com.yc.spring02.Student" p:sid="yc002" p:name="yc" p:sex="男" autowire="constructor">bean> <bean id="addr03" class="com.yc.spring02.Address" p:country="湖南" p:city="长沙" >bean> <bean id="addr04" class="com.yc.spring02.Address" parent="addr03" p:city="衡阳">bean> <bean id="student10" class="com.yc.spring02.Student" p:sid="yc002" p:name="yc" p:sex="男" autowire="byName" depends-on="address">bean> <bean id="student14" class="com.yc.spring02.Student" p:sid="yc002" p:name="yc" p:sex="男" p:address-ref="addr01" scope="prototype">bean>
当然,在beans.xml中还可以引用另一个xml配置文件:
<import resource="list.xml" />