一、普通属性:
1、定义一个properties文件:
name=\u6211\u53EA\u7231\u4F60
sex=\u7537
age=28
2、定义一个类
import java.util.Date;
publicclass personImpl {
private String name;
private String sex;
private Integer age;
private Date brith;
public personImpl() {
super();
// TODO Auto-generatedconstructor stub
}
public personImpl(Stringname, String sex, Integer age, Date brith) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.brith = brith;
}
publicvoid setName(String name) {
this.name = name;
}
publicvoid setSex(String sex) {
this.sex = sex;
}
publicvoid setAge(Integer age) {
this.age = age;
}
publicvoid setBrith(Date brith) {
this.brith = brith;
}
}
3、在xml文件中配置
<bean id="personimpl"class="cn.csdn.hr.service.personImpl">
<property name="name">
<value>${name}</value>
</property>
<property name="sex">
<value>${sex}</value>
</property>
<property name="age">
<value>12</value>
</property>
<property name="brith">
<bean class="java.util.Date"/>
</property>
<!-- 分散配置解析器 -->
//第一种写法
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>person.properties</value>
</property>
</bean>
//第二种:引用contex标签
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:property-placeholderlocaltion="文件名.properties"/>
</beans>
二、对于特殊bean来说
1、定义一个类address
packagecn.csdn.hr.service;
publicclass Address {
private String province;
private String city;
private String street;
private String zipCode;
public Address() {
super();
// TODO Auto-generated constructor stub
}
publicvoid setProvince(String province) {
this.province = province;
}
publicvoid setCity(String city) {
this.city = city;
}
publicvoid setStreet(String street) {
this.street = street;
}
publicvoid setZipCode(String zipCode) {
this.zipCode = zipCode;
}
@Override
public String toString() {
return"Address [province=" + province + ", city=" + city + ", street="+ street + ",zipCode=" + zipCode + "]";
}
}
2、定义一个类、该类的属性的类型是类address
packagecn.csdn.hr.service;
import java.util.Date;
publicclass personImpl {
private Address homeAddress;
private Address comAddress;
public personImpl() {
super();
// TODO Auto-generated constructor stub
}
public personImpl(Address homeAddress, Address comAddress) {
super();
this.homeAddress = homeAddress;
this.comAddress = comAddress;
}
public Address getHomeAddress() {
returnhomeAddress;
}
publicvoid setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
public Address getComAddress() {
returncomAddress;
}
publicvoid setComAddress(Address comAddress) {
this.comAddress = comAddress;
}
}
3、在xml中
<property name="homeAddress">
<bean class="cn.csdn.hr.service.Address">
<property name="province">
<value>北京</value>
</property>
<property name="city">
<value>北京市</value>
</property>
<property name="street">
<value>海淀区0008街道</value>
</property>
<property name="zipCode">
<value>111111</value>
</property>
</bean>
</property>
<property name="comAddress">
<value>河南省.商丘市.梁园区0008街道.100000</value>
</property>
<!-- 定制编辑器后处理类 -->
<bean id="customEditorConfigurer"class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="cn.csdn.hr.service.Address" value="cn.csdn.hr.editor.AddressEditor"/>
</map>
</property>
</bean>