CustomEditorConfigurer and ConfigurableListableBeanFactory : ConfigurableListableBeanFactory « Spring « Java






CustomEditorConfigurer and ConfigurableListableBeanFactory

       
File: context.xml

<?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="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <entry key="java.lang.Double">
                    <bean class="ComplexPropertyEditor"/>
                </entry>
            </map>
        </property>
    </bean>

    <bean id="exampleBean" class="CustomEditorDemo">
        <property name="values">
            <list>
                <value>10</value>
                <value>-10</value>
                <value>10</value>
            </list>
        </property>
    </bean>

</beans>


File: Main.java

import java.beans.PropertyEditorSupport;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Main {
  public static void main(String[] args) throws Exception {
    ConfigurableListableBeanFactory factory = new XmlBeanFactory(new ClassPathResource(
        "context.xml"));

    CustomEditorConfigurer configurer = (CustomEditorConfigurer) factory
        .getBean("customEditorConfigurer");
    configurer.postProcessBeanFactory(factory);

    CustomEditorDemo bean = (CustomEditorDemo) factory.getBean("exampleBean");

    System.out.println(bean.sum());
  }
}

class ComplexPropertyEditor extends PropertyEditorSupport {
  public void setAsText(String s) throws IllegalArgumentException {
    Double re = Double.parseDouble(s);
  }
}

class CustomEditorDemo {
  private double[] values;
  private double result;
  public double sum() {
    for (double value : this.values) {
      result = result + value;
    }
    return result;
  }

  public void setValues(double[] values) {
    this.values = values;
  }

}




           
       








Spring-ConfigurableListableBeanFactory.zip( 2,600 k)

Related examples in the same category

1.Use ConfigurableListableBeanFactory