分散配置的一个案例

来源:互联网 发布:单片机教程视频 编辑:程序博客网 时间:2024/06/11 19:58

packagecn.csdn.hr.service;

publicclass Address {

    private Stringprovince;

    private Stringcity;

    private Stringstreet;

    private StringzipCode;

    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文件

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

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

  <propertyname="location">

        <value>person.properties</value>

    </property>

   

     <propertyname="locations">

          <list>

             <value>person.properties</value>

          </list>

     </property>

  </bean>

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

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

      <propertyname="customEditors">

         <map>

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

             <entrykey="cn.csdn.hr.service.Address">

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

                  <beanclass="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

         }

}

原创粉丝点击