分散配置总结


一、普通属性:

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值