1. 程式人生 > >Java spring自定義屬性編輯器

Java spring自定義屬性編輯器

匯入測試jar包junit-4.8.2.jar

字串自定義Date轉化

1.新增分拆配置檔案applicationContext_editor.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">



    <!-- id是唯一性標識 -->
    <bean id="customEditors" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <entry key="java.util.Date"  value ="com.xiaoma.demo.UtilDatePropertyEditor"  />
            </map>
        </property>

    </bean>


</beans>

第一個配置檔案applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">



    <!-- id是唯一性標識 -->
    <bean id="bean1" class="com.xiaoma.demo.Bean1 ">
        <property name="name" value="xiaoliu" />

        <property name="dateValue" value="2018-1-15"></property>

    </bean>

</beans>

建立實體Bean1

package com.xiaoma.demo;

import java.awt.List;
import java.util.Date;
import java.util.Map;
import java.util.Set;

public class Bean1 {

    private String name;

    private int age;

    private List lists; 

    private Set set;

    private String [] arrys;

    private Map maps;

    private Date  dateValue;



    public Date getDateValue() {
        return dateValue;
    }

    public void setDateValue(Date dateValue) {
        this.dateValue = dateValue;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public List getLists() {
        return lists;
    }

    public void setLists(List lists) {
        this.lists = lists;
    }

    public Set getSet() {
        return set;
    }

    public void setSet(Set set) {
        this.set = set;
    }

    public String[] getArrys() {
        return arrys;
    }

    public void setArrys(String[] arrys) {
        this.arrys = arrys;
    }

    public Map getMaps() {
        return maps;
    }

    public void setMaps(Map maps) {
        this.maps = maps;
    }




}

建立自定義屬性編輯器 UtilDatePropertyEditor

package com.xiaoma.demo;

import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * AOP宣告式服務能力
 * 對於資源,Hibernate Session  或JDBC Connection 我們不負責開啟和關閉
 * 面向介面程式設計
 * 減少程式碼中的耦合,將耦合配置到配置檔案之中
 * 
 * @author Sugar
 * 時間轉換器,將字串型別轉化為java.util.Date
 *
 */
public class UtilDatePropertyEditor extends PropertyEditorSupport{


    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        System.out.println(text);

        try {
            Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(text);

            this.setValue(parse);
        } catch (Exception e) {
            // TODO: handle exception
        } 
    }


}

運用測試類ShowTest

package test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xiaoma.demo.Bean1;

import junit.framework.TestCase;

public class ShowTest extends TestCase{


    private BeanFactory factory;
    @Override
    protected void setUp() throws Exception {
        //多配置檔案的載入
        String [] configLocations = new String []{"applicationContext.xml","applicationContext_editor.xml"};

        factory = new ClassPathXmlApplicationContext(configLocations);
    }



    public void testShow(){
        Bean1 bean1 = (Bean1) factory.getBean("bean1");
        String name = bean1.getName();
        System.out.println("name:"+name);
    }

    @Override
    protected void tearDown() throws Exception {
        // TODO Auto-generated method stub
        super.tearDown();
    }



}

PS:使用spring4.0報錯問題

<!-- id是唯一性標識 -->
<bean id="customEditors"
    class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    <property name="customEditors">
        <map>
            <entry key="java.util.Date">
                <bean class="com.xiaoma.demo.UtilDatePropertyEditor"></bean>
            </entry>
        </map>
    </property>

</bean>

報錯Failed to convert property value of type ‘java.util.LinkedHashMap’ to required type ‘java.util.Map’ for property ‘customEditors’;

是由於2.0api與4.0api的區別,在4.0之中改為如下

<!-- id是唯一性標識 -->
    <bean id="customEditors" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <entry key="java.util.Date"  value ="com.xiaoma.demo.UtilDatePropertyEditor"  />
            </map>
        </property>

    </bean>

報錯問題解決