分散配置的一个案例

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

    }

    Setget方法

    @Override

    public String toString() {

        return"Address [province=" + province + ",city=" + city + ", street="

                + street + ", zipCode=" + zipCode + "]";

    }

}

 

 

package cn.csdn.hr.editor;

import java.beans.PropertyEditorSupport;

importorg.springframework.beans.factory.config.CustomEditorConfigurer;

import cn.csdn.hr.service.Address;

//继承的java.beans的类

public class AddressEditor extendsPropertyEditorSupport {

         @Override

         publicvoid setAsText(String text) throws IllegalArgumentException {

              //Java实现转换

             if(!"".equals(text)||text!=null){

                       String args[] = text.split("\\.");

                       if(args.length>3){

                                 Address address = new Address();

                                 address.setProvince(args[0]);

                                 address.setCity(args[1]);

                                 address.setStreet(args[2]);

                                 address.setZipCode(args[3]);

                      setValue(address);

                       }else{

                                 this.setAsText(null);

                       }

              }else{

                       this.setAsText(null);

              }

                  

                   //CustomEditorConfigurer

         }

}

配置xml文件

<!--分散配置解析的后处理类 -->

  <bean id="propertyPlaceholderConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

  <property name="location">

        <value>person.properties</value>

    </property>

   

     <property name="locations">

          <list>

             <value>person.properties</value>

          </list>

     </property>

  </bean>

  <!-- 定制编辑器后处理类 -->

  <bean id="customEditorConfigurer"class="org.springframework.beans.factory.config.CustomEditorConfigurer">

      <property name="customEditors">

         <map>

             <!-- key指向的是:需要转换的类 -->

             <entry key="cn.csdn.hr.service.Address">

                   <!-- value指向的是实现的编辑器类 -->

                  <bean class="cn.csdn.hr.editor.AddressEditor"/>

             </entry>

         </map>

      </property>

  </bean>

  测试类

package cn.csdn.hr.junit;

import static org.junit.Assert.*;

import org.junit.Test;

importorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

importcn.csdn.hr.service.PersonServiceBean;

public class AppMain {

         @Test

         publicvoid test() {

                   //第一步:获取应用程序上下文对象 //

                                     ApplicationContextac = new ClassPathXmlApplicationContext("classpath:beans.xml");

                                     //第二步:根据应用程序上下文对象的getBean("Id名称")获取bean的实例对象

                                     PersonServiceBeanpersonServiceBean =  (PersonServiceBean)ac.getBean("personServiceBean");

        

                   System.out.println(personServiceBean.toString());

                   

                   System.out.println(personServiceBean.getHomeAddress().toString());

                   

                    System.out.println("利用自己定制属性编辑器实现");

                   System.out.println(personServiceBean.getComAddress().toString());

                 //  PropertyPlaceholderConfigurer

         }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值