spring自定义属性编辑器使用dome

来源:互联网 发布:2017年10月非农数据 编辑:程序博客网 时间:2024/06/05 19:11
Spring xml配置
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>
<ref bean="customCollectionEditorRegistrar"/>
</list>
</property>
</bean>

<bean id="customCollectionEditorRegistrar" class="com.focustech.channelfy.product.service.factoryBean.CustomCollectionEditorRegistrar">
<property name="factoryListEditor" ref="factoryListEditor"/>
</bean>

<bean id="factoryListEditor" class="org.springframework.beans.propertyeditors.CustomCollectionEditor">
<constructor-arg name="collectionType" value="com.focustech.channelfy.product.service.factoryBean.FactoryArrayList" type="java.lang.Class"/>
</bean>

CustomCollectionEditorRegistrar自定义注册机
public class CustomCollectionEditorRegistrar implements PropertyEditorRegistrar {

private PropertyEditor factoryListEditor;

public void setFactoryListEditor(PropertyEditor factoryListEditor) {
this.factoryListEditor = factoryListEditor;
}

@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(FactoryList.class, factoryListEditor);
}
}

FactoryArrayList需要自定属性
public class FactoryArrayList<T extends MatchingBean<K>,K> extends ArrayList<T> implements FactoryList<T,K> {

public T getBean(K k) throws NoFindClassException {
Iterator<T> iterator = this.iterator();
while (iterator.hasNext()){
T next = iterator.next();
if (next.getBean(k)){
return next;
}
}
throw new NoFindClassException();
}
}

自定属性实现接口
public interface FactoryList<E extends MatchingBean<K>,K> extends List<E> {
E getBean(K var1) throws NoFindClassException;
}

自定属性实现接口
public interface MatchingBean<T> {
boolean getBean(T t);
}
原创粉丝点击