dependency check Demo : Dependency Injection « Spring « Java Tutorial






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="target1" class="SimpleBean"
            dependency-check="simple">
        <property name="someInt" value="1"/>
    </bean>

    <bean id="target2" class="SimpleBean"
        dependency-check="objects">
        <property name="nestedSimpleBean" ref="nestedSimpleBean"/>
    </bean>

    <bean id="target3" class="SimpleBean"
        dependency-check="all">
        <property name="nestedSimpleBean" ref="nestedSimpleBean"/>
        <property name="someInt" value="1"/>
    </bean>

    <bean id="nestedSimpleBean" class="SimpleBean"/>

</beans>

File: Main.java

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

public class Main {

  public static void main(String[] args) {
    XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("context.xml"));

    System.out.println(bf.getBean("target1"));
    System.out.println(bf.getBean("target2"));
    System.out.println(bf.getBean("target3"));
  }
}

class SimpleBean {
  private int someInt;

  private SimpleBean nestedSimpleBean;

  public void setSomeInt(int someInt) {
    this.someInt = someInt;
  }

  public void setNestedSimpleBean(SimpleBean nestedSimpleBean) {
    this.nestedSimpleBean = nestedSimpleBean;
  }

  @Override
  public String toString() {
    final StringBuilder sb = new StringBuilder();
    sb.append("SimpleBean");
    sb.append("{someInt=").append(someInt);
    sb.append(", nestedSimpleBean=").append(nestedSimpleBean);
    sb.append('}');
    return sb.toString();
  }
}
  Download:  Spring-dependencycheckDemo.zip( 2,599 k)








28.8.Dependency Injection
28.8.1.Dependency Pull Demo
28.8.2.dependency check Demo
28.8.3.Contextualized Dependency Lookup Demo
28.8.4.Spring bean dependency Demo
28.8.5.Setter Dependency Injection Demo
28.8.6.Dependency Injection Demo