栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring 5.2.9 07 源码分析-spring的bean工厂准备工作(下)测试用例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring 5.2.9 07 源码分析-spring的bean工厂准备工作(下)测试用例

如何扩展实现自定义的属性编辑器

  1. 自定义一个实现了PropertyEditorSupport接口的编辑器
  2. 让spring能够识别到此编辑器,自定义实现一个属性编辑器的注册器,实现了PropertyEditorRegistrar接口
  3. 让spring能够识别到对应的注册器

导航,文件结构

程序入口

package com.mashibing;

import com.mashibing.selfEditor.Customer;

public class TestCustomer {

    //01 源码分析-spring的bean工厂准备工作.vep
    //prepareBeanfactory
    public static void main(String[] args) {
        MyClassPathXmlApplicationContext ac = new MyClassPathXmlApplicationContext("selfEditor.xml");
        Customer bean = ac.getBean(Customer.class);
        System.out.println(bean);


    }

}

Address类解析器注册

package com.mashibing.selfEditor;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;

public class AddressPropertyEditorRegistrar implements PropertyEditorRegistrar {

    @Override
    public void registerCustomEditors(PropertyEditorRegistry registry) {
        registry.registerCustomEditor(Address.class,new AddressPropertyEditor());
    }
}

Address类解析器,将address值转换为Address类

package com.mashibing.selfEditor;

import java.beans.PropertyEditorSupport;

public class AddressPropertyEditor  extends PropertyEditorSupport {

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        String[] s = text.split("_");
        Address address = new Address();
        address.setProvince(s[0]);
        address.setCity(s[1]);
        address.setTown(s[2]);
        this.setValue(address);
    }
}

selfEditor.xml 有两种方式注入值




    
        
        
    
    
        
            
                
            
        
    












测试用例
https://download.csdn.net/download/weixin_50750933/85749576

相关类
CustomEditorConfigurer.java
注意查看此文件最后一个方法postProcessBeanFactory,可以选择两种方式进行注入。


package org.springframework.beans.factory.config;

import java.beans.PropertyEditor;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.core.Ordered;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;


public class CustomEditorConfigurer implements BeanFactoryPostProcessor, Ordered {

	protected final Log logger = LogFactory.getLog(getClass());

	private int order = Ordered.LOWEST_PRECEDENCE;  // default: same as non-Ordered

	@Nullable
	private PropertyEditorRegistrar[] propertyEditorRegistrars;

	@Nullable
	private Map, Class> customEditors;


	public void setOrder(int order) {
		this.order = order;
	}

	@Override
	public int getOrder() {
		return this.order;
	}

	
	public void setPropertyEditorRegistrars(PropertyEditorRegistrar[] propertyEditorRegistrars) {
		this.propertyEditorRegistrars = propertyEditorRegistrars;
	}

	
	public void setCustomEditors(Map, Class> customEditors) {
		this.customEditors = customEditors;
	}


	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		// 如果属性编辑注册器不等于空
		if (this.propertyEditorRegistrars != null) {
			// 遍历属性编辑注册器的集合
			for (PropertyEditorRegistrar propertyEditorRegistrar : this.propertyEditorRegistrars) {
				// 将属性编辑注册器添加到beanFactory
				beanFactory.addPropertyEditorRegistrar(propertyEditorRegistrar);
			}
		}
		// 如果自定义编辑器不等于空
		if (this.customEditors != null) {
			// 遍历自定义编辑器集合将自定义编辑器添加到beanFactory中
			this.customEditors.forEach(beanFactory::registerCustomEditor);
		}
	}

}

疑问:editor通过PropertyEditorRegistrySupport.customEditors属性获得自定义解析器。
如果我注入的是propertyEditorRegistrar,则实在那里转换为customEditors的。
package org.springframework.beans;
TypeConverterDelegate.convertIfNecessaryconvertIfNecessary(@Nullable String propertyName, @Nullable Object oldValue, @Nullable Object newValue,
@Nullable Class requiredType, @Nullable TypeDescriptor typeDescriptor) throws IllegalArgumentException {}
PropertyEditor editor = this.propertyEditorRegistry.findCustomEditor(requiredType, propertyName);

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/988436.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号