Java Tutorial/Spring/Xml Bean Factory — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:22, 31 мая 2010

Add addBeanPostProcessor To XmlBeanFactory

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="w" class="java.lang.String">
  </bean>
 
  <bean id="beanInitLogger" class="BeanInitializationLogger"/>
</beans>





Add BeanFactory PostProcessor To XmlBeanFactory

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="w" class="java.lang.String">
  </bean>
 
  <bean id="allBeansLister" class="AllBeansLister"/>
</beans>





BeanFactory Aware

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="logging" class="LoggingBean"/>
    <bean class="ShutdownHookBean"/>
</beans>





Compare Beans From Factory Bean

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="sha" class="MessageDigestFactoryBean">
        <property name="algorithm" value="SHA1"/>
    </bean>
    <bean id="md5" class="MessageDigestFactoryBean"/>
    
</beans>





Create XmlBeanFactory from ClassPathResource

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="logging" class="LoggingBean"/>
</beans>





Hierarchical BeanFactory Demo

File: injectdemo2-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="name" class="java.lang.String">
        <constructor-arg value="My Value 1"/>
    </bean>
    <bean id="injectSimpleParent" class="InjectSimpleDemo">
        <property name="name" ref="name"/>
        <property name="age" value="2"/>
        <property name="height" value="0.8"/>
        <property name="isProgrammer" value="false"/>
        <property name="ageInSeconds" value="63072000"/>
    </bean>
</beans>





Non Static Factory

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="nonStaticFactory" class="NonStaticFactory"/>
  <bean id="testBeanObtainedViaNonStaticFactory"
        factory-bean="nonStaticFactory" factory-method="getTestBeanInstance"/>
</beans>





Static Factory

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="testBeanObtainedViaStaticFactory"
        class="StaticFactory" factory-method="getTestBeanInstance"/>

</beans>





Xml Bean Factory Demo

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