Xml Bean Factory Demo : Xml Bean Factory « Spring « Java Tutorial






File: Main.java

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Main {
  public static void main(String[] args) throws Exception {
    XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    System.out.println(factory.getBean("simple1"));
  }

}

class SoutSimpleBean extends SimpleBeanSupport {
  private String person;

  public void setPerson(String person) {
    this.person = person;
  }

  @Override
  public String toString() {
    return String.format("%s : \"%s\"", this.person, getValue());
  }
}

abstract class SimpleBeanSupport implements InitializingBean {
  private String value;

  public final void afterPropertiesSet() throws Exception {
  }

  public final void setValue(String value) {
    this.value = value;
  }

  protected final String getValue() {
    return this.value;
  }
}

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="simple1" class="SoutSimpleBean">
        <property name="person" value="A"/>
        <property name="value" value="my value"/>
    </bean>

</beans>
  Download:  Spring-XmlBeanFactoryDemo.zip( 2,599 k)








28.6.Xml Bean Factory
28.6.1.Xml Bean Factory Demo
28.6.2.Static Factory
28.6.3.Compare Beans From Factory Bean
28.6.4.Create XmlBeanFactory from ClassPathResource
28.6.5.BeanFactory Aware
28.6.6.Non Static Factory
28.6.7.Hierarchical BeanFactory Demo
28.6.8.Add BeanFactory PostProcessor To XmlBeanFactory
28.6.9.Add addBeanPostProcessor To XmlBeanFactory