我是靠谱客的博主 酷炫刺猬,这篇文章主要介绍Spring中的可扩展点(二)自定义属性编辑器,现在分享给大家,希望可以做个参考。

目录

1.自定义属性编辑器具体步骤

2.DEMO例程

2.1 创建Address类

2.2 创建Customer类,依赖于Address

2.3 创建自定义Editor

2.4 创建自定义Registrar

2.5 创建customizeEditor.xml配置文件

2.6 Application

2.7 结果演示

3.Spring是如何做到可扩展的


1.自定义属性编辑器具体步骤

  1. 自定义一个实现了PropertyEditorSupport接口的编辑器,重写setAsText方法。
  2. 让spring能够识别到这个自定义的编辑器,自定义一个属性编辑器的注册器,即实现PropertyEditorRegister接口
  3. 让spring能够识别到对应的注册器

2.DEMO例程

2.1 创建Address类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.zoo.customize.editor; /** * @author Liu XiaoBai */ public class Address { private String province; private String city; private String town; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getTown() { return town; } public void setTown(String town) { this.town = town; } @Override public String toString() { return "Address{" + "province='" + province + ''' + ", city='" + city + ''' + ", town='" + town + ''' + '}'; } }

2.2 创建Customer类,依赖于Address

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.zoo.customize.editor; /** * @author Liu XiaoBai */ public class Customer { private String name; private Address address; public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "Customer{" + "name='" + name + ''' + ", address=" + address + '}'; } }

2.3 创建自定义Editor

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.zoo.customize.editor; import java.beans.PropertyEditorSupport; /** * @author Liu XiaoBai */ 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); } }

2.4 创建自定义Registrar

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.zoo.customize.editor; import org.springframework.beans.PropertyEditorRegistrar; import org.springframework.beans.PropertyEditorRegistry; /** * @author Liu XiaoBai */ public class AddressPropertyEditorRegistrar implements PropertyEditorRegistrar { @Override public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Address.class,new AddressPropertyEditor()); } }

2.5 创建customizeEditor.xml配置文件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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.xsd"> <bean id="customer" class="com.zoo.customize.editor.Customer"> <property name="name" value="tom"/> <property name="address" value="河南省_漯河市_舞阳县"/> </bean> <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="propertyEditorRegistrars"> <list> <bean class="com.zoo.customize.editor.AddressPropertyEditorRegistrar"/> </list> </property> </bean> </beans>

2.6 Application

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.zoo; import com.zoo.customize.editor.Customer; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author liuxiaobai */ public class Application { public static void main(String[] args) { AbstractApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml","customizeXsd.xml","customizeEditor.xml"); Customer customer = (Customer)ac.getBean("customer"); System.out.println(customer); ac.registerShutdownHook(); } }

2.7 结果演示

3.Spring是如何做到可扩展的

        首先自定义的AddressPropertyEditorRegistrar将Address.class类的解析工作委托给了AddressPropertyEditor。然后customizeEditor.xml中将AddressPropertyEditorRegistrar添加到了CustomEditorConfigurer类propertyEditorRegistrars属性中,而CustomEditorConfigurer实现了BeanFactoryPostProcessor的,这样在集体处理BeanFactoryPostProcessors的时候就可以将程序中的AddressPropertyEditorRegistrar添加到Spring容器中。

        具体注册AddressPropertyEditorRegistrar是在AbstractBeanFactory#initBeanWrapper中:

        跟进此方法发现进行了自定义Editor的注册:

        具体调用堆栈:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
registerCustomEditors:1246, AbstractBeanFactory (org.springframework.beans.factory.support) initBeanWrapper:1234, AbstractBeanFactory (org.springframework.beans.factory.support) instantiateBean:1323, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) createBeanInstance:1223, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) doCreateBean:559, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) createBean:517, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) lambda$doGetBean$0:332, AbstractBeanFactory (org.springframework.beans.factory.support) getObject:-1, 769429195 (org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$8) getSingleton:238, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support) doGetBean:330, AbstractBeanFactory (org.springframework.beans.factory.support) getBean:208, AbstractBeanFactory (org.springframework.beans.factory.support) invokeBeanFactoryPostProcessors:172, PostProcessorRegistrationDelegate (org.springframework.context.support) invokeBeanFactoryPostProcessors:766, AbstractApplicationContext (org.springframework.context.support) refresh:549, AbstractApplicationContext (org.springframework.context.support) <init>:145, ClassPathXmlApplicationContext (org.springframework.context.support) <init>:95, ClassPathXmlApplicationContext (org.springframework.context.support) main:13, Application (com.zoo)

        解析的入口是在AbstractAutowireCapableBeanFactory#populateBean():

        最终一步步的会调用到我们自定义的编辑器中,调用堆栈:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
setAsText:11, AddressPropertyEditor (com.zoo.customize.editor) doConvertTextValue:429, TypeConverterDelegate (org.springframework.beans) doConvertValue:402, TypeConverterDelegate (org.springframework.beans) convertIfNecessary:155, TypeConverterDelegate (org.springframework.beans) convertIfNecessary:590, AbstractNestablePropertyAccessor (org.springframework.beans) convertForProperty:609, AbstractNestablePropertyAccessor (org.springframework.beans) convertForProperty:219, BeanWrapperImpl (org.springframework.beans) convertForProperty:1756, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) applyPropertyValues:1712, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) populateBean:1452, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) doCreateBean:598, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) createBean:517, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support) lambda$doGetBean$0:332, AbstractBeanFactory (org.springframework.beans.factory.support) getObject:-1, 769429195 (org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$8) getSingleton:238, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support) doGetBean:330, AbstractBeanFactory (org.springframework.beans.factory.support) getBean:203, AbstractBeanFactory (org.springframework.beans.factory.support) preInstantiateSingletons:902, DefaultListableBeanFactory (org.springframework.beans.factory.support) finishBeanFactoryInitialization:939, AbstractApplicationContext (org.springframework.context.support) refresh:577, AbstractApplicationContext (org.springframework.context.support) <init>:145, ClassPathXmlApplicationContext (org.springframework.context.support) <init>:95, ClassPathXmlApplicationContext (org.springframework.context.support) main:13, Application (com.zoo)

最后

以上就是酷炫刺猬最近收集整理的关于Spring中的可扩展点(二)自定义属性编辑器的全部内容,更多相关Spring中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部