1、常见的属性的注入:int,string,list,set,map

2、什么是属性编辑器及作用?

(1)将spring配置文件中的字符串转换为相应的java对象

(2)spring内置了一些属性编辑器,也可以自定义属性编辑器

3、如果自定义属性编辑器

(1)继承propertyEditorSupport

(2)重写setAsText 方法

(3)使用 setValue 完成属性对象设置

下面通过实例来说明类属性自定义动态加载

工程截图:

工程说明:

1、Education.java 自定义属性(自定义类属性)

2、EducationPropertyEditor.java自定义属性编辑器

3、PropertyDateEditor.java 自定义日期属性编辑器

4、testPropertyEditot 单元测试类

测试结果截图:

代码:

beans.xm文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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">

<bean id="PropertyEditorConfigurer1"
        class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <!-- 引用类的属性类型,本例子中对应 com.test.Person 类的 education 属性 东北大亨对应读取的学校属性 -->
                <entry key="com.test.Education">
                    <!--对应Address的编辑器 -->
                    <bean class="com.test.EducationPropertyEditor" />
                </entry>
            </map>
        </property>
    </bean>
    <bean id="PropertyEditorConfigurer2"
        class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <!-- 引用类的属性类型,本例子中对应 com.test.Person 类的 dataValue属性 -->
                <entry key="java.util.Date">
                    <!--对应dataValue 属性的编辑器 -->
                    <bean class="com.test.PropertyDateEditor" >
                        <property name="dataPattern" value="yyyy/MM/dd"/>
                    </bean>
                </entry>
            </map>
        </property>
    </bean>
    <bean id="person" class="com.test.Person">
        <property name="id" value="1003" />
        <property name="name" value="东北大亨" />
        <property name="education" value="中国,北京海淀区清华大学" />
        <property name="dataValue" value="2018/12/30" />
    </bean>
</beans>

测试类 testPropertyEditot:

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import junit.framework.TestCase;

public class testPropertyEditot extends TestCase {
    
    public void testPrintProperty() {
        System.out.println("测试springPropertyUtil  start");

// 可以同时配置多个xml配置文件,如果多个配置具有一定规则情况下可以采用匹配方式进行读取
        // 例如有两个xml 文件。既:beans-1.xml,beans-2.xml,beans-3.xml
        // 采用匹配方式进行读取
        // ApplicationContext ctx = new
        // ClassPathXmlApplicationContext("beans-*.xml");

// 废弃方法不建议使用
        // BeanFactory factory=new XmlBeanFactory(new
        // ClassPathResource("beans.xml"));

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        Person person = (Person) ctx.getBean("person");

System.out.println("学生ID:" + person.getId());
        System.out.println("名称:" + person.getName());
        System.out.println("毕业时间:" + person.getDataValue());
        System.out.println("学生就读国别:" + person.getEducation().getCountry());
        System.out.println("学生就读地址:" + person.getEducation().getDirectory());
        
        assertEquals(person.getId(),1003);
        assertEquals(person.getName(),"东北大亨");

System.out.println("测试springPropertyUtil  end");
    }
}

日期编辑器 PropertyDateEditor :

package com.test;

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

/**
 *
 * @author 东北大亨
 *
 */
public class PropertyDateEditor extends PropertyEditorSupport {
    
    
    private String dataPattern;

/**
     * Parse the value from the given text, using the SimpleDateFormat
     */
    @Override
    public void setAsText(String text) {
        System.out.println("------UtilDatePropertyEditor.setAsText()------" + text);
        try {
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf = new SimpleDateFormat(dataPattern);
            Date date = new Date();
            date = sdf.parse(text);
            this.setValue(date);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException(text);
        }
    }
    
    // 只要有set方法就可以注入进来
    public void setDataPattern(String pattern) {
        this.dataPattern = pattern;
    }
}

人员 Person pojo类:

package com.test;

import java.util.Date;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 *
 * @author 东北大亨
 *
 */
public class Person {

private int id;
    private String name;
    private Education education;

private Date dataValue;
    
    public int getId() {
        return id;
    }

public void setId(int id) {
        this.id = id;
    }

public Date getDataValue() {
        return dataValue;
    }

public void setDataValue(Date dataValue) {
        this.dataValue = dataValue;
    }

public Education getEducation() {
        return education;
    }

public void setEducation(Education education) {
        this.education = education;
    }

public String getName() {
        return name;
    }

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

public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

实体类属性编辑器:

package com.test;

import java.beans.PropertyEditorSupport;
import org.springframework.util.StringUtils;

/**
 * 实体类编辑器
 * @author 东北大亨
 *
 */
public class EducationPropertyEditor extends PropertyEditorSupport {

@Override
    public void setAsText(String text) {
        if (text == null || !StringUtils.hasText(text)) {
            throw new IllegalArgumentException("就读住址不能为空!");
        } else {
            String[] StrAddressArr = StringUtils.tokenizeToStringArray(text, ",");
            Education add = new Education();
            add.setCountry(StrAddressArr[0]);
            add.setDirectory(StrAddressArr[1]);
            setValue(add);
        }
    }

public String getAsText() {
        Education add = (Education) getValue();
        return "" + add;
    }
}

学校实体类:

package com.test;

import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * 读取学校实体类
 * @author 东北大亨
 *
 */
public class Education {

private String country;

private String directory;

public String getDirectory() {
        return directory;
    }

public void setDirectory(String directory) {
        this.directory = directory;
    }

public String getCountry() {
        return country;
    }

public void setCountry(String country) {
        this.country = country;
    }

}

Spring属性编辑器详解的更多相关文章

  1. spring事务配置详解

    一.前言 好几天没有在对spring进行学习了,由于这几天在赶项目,没有什么时间闲下来继续学习,导致spring核心架构详解没有继续下去,在接下来的时间里面,会继续对spring的核心架构在继续进行学 ...

  2. spring注入参数详解

    spring注入参数详解 在Spring配置文件中, 用户不但可以将String, int等字面值注入到Bean中, 还可以将集合, Map等类型的数据注入到Bean中, 此外还可以注入配置文件中定义 ...

  3. Spring的lazy-init详解

    1.Spring中lazy-init详解ApplicationContext实现的默认行为就是在启动服务器时将所有singleton bean提前进行实例化(也就是依赖注入).提前实例化意味着作为初始 ...

  4. Spring Security Filter详解

    Spring Security Filter详解 汇总 Filter 作用 DelegatingFilterProxy Spring Security基于这个Filter建立拦截机制 Abstract ...

  5. Spring Boot 配置文件详解

    Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...

  6. spring原理案例-基本项目搭建 02 spring jar包详解 spring jar包的用途

    Spring4 Jar包详解 SpringJava Spring AOP: Spring的面向切面编程,提供AOP(面向切面编程)的实现 Spring Aspects: Spring提供的对Aspec ...

  7. (转)Spring事务管理详解

    背景:之前一直在学习数据库中的相关事务,而忽略了spring中的事务配置,在阿里面试时候基本是惨败,这里做一个总结. 可能是最漂亮的Spring事务管理详解 https://github.com/Sn ...

  8. spring事务管理(详解和实例)

    原文地址: 参考地址:https://blog.csdn.net/yuanlaishini2010/article/details/45792069 写这篇博客之前我首先读了<Spring in ...

  9. 可能是最漂亮的Spring事务管理详解

    Java面试通关手册(Java学习指南):https://github.com/Snailclimb/Java_Guide 微信阅读地址链接:可能是最漂亮的Spring事务管理详解 事务概念回顾 什么 ...

随机推荐

  1. C#Qrcode生成二维码支持中文,带图片,带文字

    C#Qrcode生成二维码支持中文带图片的操作请看二楼的帖子,当然开始需要下载一下C#Qrcode的源码 下载地址 : http://www.codeproject.com/Articles/2057 ...

  2. 【转】awk的使用及字符串的操作

    awk教程 转载[awk的使用及字符串的操作] sed版: sed '1~2d' file #删除奇数行 sed '1~2!d' file #删除偶数行 awk版: awk 'NR%2==0' fil ...

  3. Light oj 1095 - Arrange the Numbers (组合数学+递推)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题意: 给你包含1~n的排列,初始位置1,2,3...,n,问你刚好固定 ...

  4. UVA 11389 The Bus Driver Problem 贪心水题

    题目链接:UVA - 11389 题意描述:有n个司机,n个早班路线和n个晚班路线,给每个司机安排一个早班路线和一个晚班路线,使得每个早班路线和晚班路线只属于一个司机.如果一个司机早班和晚班总的驾驶时 ...

  5. JDK1.5新特性:

    1.自动装箱与拆箱: 自动装箱的过程:每当需要一种类型的对象时,这种基本类型就自动地封装到与它相同类型的包装中. 自动拆箱的过程:每当需要一个值时,被装箱对象中的值就被自动地提取出来,没必要再去调用i ...

  6. x86服务器中网络性能分析与调优 转

    x86服务器中网络性能分析与调优 2017-04-05 巨枫 英特尔精英汇 [OpenStack 易经]是 EasyStack 官微在2017年新推出的技术品牌,将原创技术干货分享给您,本期我们讨论 ...

  7. Meet Dgraph — an open source, scalable, distributed, highly available and fast graph databas

    https://dgraph.io/ Meet Dgraph — an open source, scalable, distributed, highly available and fast gr ...

  8. mysqldump实践

    mysqldump   mysqldump---逻辑备份,热备 单线程,适合数据量小的库 mysql官方自带的命令行工具   #全库 )mysqldump -uroot -p123456 --sock ...

  9. linux命令lsattr、chattr、man

    1.man命令,可以查看手册 配置位置/etc/man.conf MANPATH决定手册查询位置 MANSECT决定man查询的顺序 man的查询 linux man的常用用法: man sectio ...

  10. CityEngine结合ArcGIS制作地质体web场景 [转]

    CityEnginewebWebWEB地质体 原文地址:http://www.cnblogs.com/esrichina/archive/2012/11/12/2762163.html 当我们想要对地 ...