Spring属性编辑器详解

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;
    }

}

 

转载于:https://www.cnblogs.com/northeastTycoon/p/spring.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security是一个用于保护Java应用程序的框架,它提供了一系列过滤器来处理安全相关的任务。在Spring Security中,过滤器被组织成一个过滤器链(Filter Chain),它根据配置的顺序依次执行。以下是一些常用的Spring Security过滤器及其功能: 1. SecurityContextPersistenceFilter:用于在请求处理期间存储和检索SecurityContext,即当前用户的安全上下文。 2. LogoutFilter:处理用户注销请求,并清除当前用户的认证信息。 3. UsernamePasswordAuthenticationFilter:用于处理基于用户名和密码的认证请求。 4. ConcurrentSessionFilter:用于处理并发会话控制,限制同一用户的同时登录数。 5. BasicAuthenticationFilter:用于处理基于HTTP基本身份验证的认证请求。 6. RequestCacheAwareFilter:用于缓存请求,以便在重定向后重新发送请求。 7. SecurityContextHolderAwareRequestFilter:用于包装请求,使其能够识别特定的安全上下文。 8. AnonymousAuthenticationFilter:用于在请求上下文中添加匿名用户的认证信息。 9. SessionManagementFilter:用于处理会话管理,例如过期检查、并发控制等。 10. ExceptionTranslationFilter:用于捕获并处理异常,将其转换为合适的响应。 11. FilterSecurityInterceptor:用于实施访问控制,检查用户是否具有访问资源的权限。 这些过滤器可以根据具体的安全需求进行配置和组合,以提供所需的安全功能。通过指定不同的过滤器顺序、添加自定义过滤器或替换默认过滤器,您可以灵活地定制Spring Security的过滤器链。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值