Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Thursday, November 22, 2007

How to use Hierarchical Bean Factory in java

/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/


///////////////////////////////////////////////////////////////////////////////////////
//File: beans.xml
























Bean In Child





///////////////////////////////////////////////////////////////////////////////////////
//File: parent.xml




Bean In Parent




Bean In Parent





///////////////////////////////////////////////////////////////////////////////////////
public class SimpleTarget {

private String val;

public void setVal(String val) {
this.val = val;
}

public String getVal() {
return val;
}
}
///////////////////////////////////////////////////////////////////////////////////////
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class HierarchicalBeanFactoryUsage {

public static void main(String[] args) {
BeanFactory parent = new XmlBeanFactory(new FileSystemResource(
"build/parent.xml"));
BeanFactory child = new XmlBeanFactory(new FileSystemResource(
"build/beans.xml"), parent);

SimpleTarget target1 = (SimpleTarget) child.getBean("target1");
SimpleTarget target2 = (SimpleTarget) child.getBean("target2");
SimpleTarget target3 = (SimpleTarget) child.getBean("target3");

System.out.println(target1.getVal());
System.out.println(target2.getVal());
System.out.println(target3.getVal());
}
}

No comments: