Java/Spring/XmlBeanFactory

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

BeanFactoryAware and BeanAware

       
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>

File: Main.java
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
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"));
    factory.preInstantiateSingletons();
    LoggingBean lb = (LoggingBean) factory.getBean("logging");
    lb.run();
  }
}
class LoggingBean implements BeanNameAware {
  private String name;
  public void setBeanName(String name) {
    this.name = name;
  }
  public void run() {
    System.out.println("Bean name is"" + this.name + "".");
  }
}
class ShutdownHookBean implements BeanFactoryAware, Runnable {
  private ConfigurableListableBeanFactory beanFactory;
  public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
      if (beanFactory instanceof DefaultListableBeanFactory) {
          this.beanFactory = (ConfigurableListableBeanFactory)beanFactory;
          Runtime.getRuntime().addShutdownHook(new Thread(this));
      }
  }
  public void run() {
      if (this.beanFactory != null) {
          System.out.println("Destroying singletons.");
          this.beanFactory.destroySingletons();
      }
  }
}





Create XmlBeanFactory from ClassPathResource

       
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="nameClass"
         class="InitializingBeanNameClass"/>
</beans>

File: Main.java
import org.springframework.beans.factory.BeanFactory;
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 {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    InitializingBeanNameClass nameClass = (InitializingBeanNameClass) beanFactory
        .getBean("nameClass");
    System.out.println(nameClass.getFirstName());
  }
}
class InitializingBeanNameClass implements InitializingBean {
  private String firstName;
  public String getFirstName() {
    return this.firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public void afterPropertiesSet() throws Exception {
    if (this.firstName == null) {
      throw new Exception("firstName property is required.");
    }
  }
}





FactoryObject Integration

       
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>
  <!--(1) -->
  <bean id="socketFactory" class="javax.net.SocketFactory" factory-method="getDefault">
  </bean>
  <bean id="localhost" 
        factory-bean="socketFactory" factory-method="createSocket">
      <constructor-arg value="localhost"/>
      <constructor-arg value="80"/>
  </bean>
  <bean id="jexp.ru" 
        factory-bean="socketFactory" factory-method="createSocket">
      <constructor-arg value="www.jexp.ru"/>
      <constructor-arg value="80"/>
  </bean>
</beans>

File: Main.java
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"));
    java.net.Socket localhost = (java.net.Socket) beanFactory.getBean("localhost");
    java.net.Socket apressDotCom = (java.net.Socket) beanFactory.getBean("jexp.ru");
    System.out.println(localhost.isConnected());
    System.out.println(apressDotCom.isConnected());
  }
}





Spring factory method

       
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="pattern" class="java.util.regex.Pattern" factory-method="compile">
    <constructor-arg value="abc"/>
  </bean>
</beans>

File: Main.java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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"));
    Pattern pattern = (Pattern) beanFactory.getBean("pattern");
    Matcher matcher = pattern.matcher("abc abc abc");
    int matchCount = 0;
    while (matcher.find()) {
      matchCount++;
    }
    System.out.println(matchCount);
  }
}





XmlBeanFactory Demo

       
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>

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