dependency check Demo : XML Bean « Spring « Java






dependency check 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="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();
  }
}




           
       








Spring-dependencycheckDemo.zip( 2,599 k)

Related examples in the same category

1.XML Bean Injection
2.Reference another bean and set property
3.Static Factory
4.Serach By Base Package
5.throw RequiredPropertyNotSetException
6.Properties File Based Spring Bean
7.Non Static Factory
8.Local Reference
9.Link With DataSource
10.Inheritance Demo
11.HierarchicalBeanFactory Demo
12.Filtered By Annotation
13.destroy method
14.Custom InitializationMethod
15.component scan
16.Component Scan and scope
17.Component Filter Assignable
18.implements BeanNameAware
19.Bean Lifecycle Initializing
20.Bean Lifecycle DisposableBean
21.Autowiring
22.Alias Bean Demo