Java/Spring/ConfigurableListableBeanFactory

Материал из Java эксперт
Версия от 06:16, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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;
  }
}





Use ConfigurableListableBeanFactory

       
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="numberClass"
       class="CustomInitializationMethodNumberClass" 
       init-method="initialize"/>
</beans>

File: Main.java
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
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 beanFactory = new XmlBeanFactory(new ClassPathResource(
        "context.xml"));
    
    CustomInitializationMethodNumberClass bean = (CustomInitializationMethodNumberClass)beanFactory.getBean("numberClass");
    
    System.out.println(bean.getNumbers());
  }
}
class CustomInitializationMethodNumberClass {
  private List<String> numbers = new ArrayList<String>();
  public List<String> getNumbers() {
    return this.numbers;
  }
  public void initialize() throws Exception {
    if (this.numbers.size() < 3) {
      numbers.add("one");
      numbers.add("two");
      numbers.add("three");
    }
  }
}