Saturday, September 12, 2009

Spring Bean Dependency

File Name: dependency.xml


File Name: Dependency.java

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


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

B b = (B) bf.getBean("b");
A a = (A) bf.getBean("a");
System.out.println(a);
System.out.println(b);
}
}

final class Shared {
private static Object value = null;
private Shared() {

}

public synchronized static void setValue(Object o) {
value = o;
}

public static Object getValue() {
return value;
}
}

class A
{
public A()
{
Shared.setValue("Undetermined");
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("A");
sb.append("{}");
sb.append("Shared.getValue()=").append(Shared.getValue()).append("}");
return sb.toString();
}
}

class B
{
public B()
{
Shared.setValue("Completed");
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("B");
sb.append("{}");
sb.append("Shared.getValue()=").append(Shared.getValue()).append("}");
return sb.toString();
}
}

No comments:

Post a Comment