spring自定義屬性編輯器使用dome
阿新 • • 發佈:2019-02-03
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);
}