快速上手Spring--7. ref的用法(5)

2008-02-23 09:30:34来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

>
Main.java
package javamxj.spring.basic.ref;

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

public class Main {

public static void main(String[] args) {
BeanFactory parent = new XmlBeanFactory(new ClassPathResource(
"javamxj/spring/basic/ref/parent.xml"));
BeanFactory child = new XmlBeanFactory(new ClassPathResource(
"javamxj/spring/basic/ref/beans.xml"), parent);

HelloDate hd1 = (HelloDate) child.getBean("hd1");
HelloDate hd2 = (HelloDate) child.getBean("hd2");
HelloDate hd3 = (HelloDate) child.getBean("hd3");

hd1.sayHello();
hd2.sayHello();
hd3.sayHello();
}
}
·运行Main.java,输出结果如下:
Hello! Parent Bean. 2005-8-10 22:25:56
Hello! Child Bean. 2005-8-10 22:25:56
Hello! Javamxj. 2005-8-10 22:25:56
OK!这里主要分析beans.xml、Main.java这两个文件。对于Main.java要注意的是如何加载“parent”的,重点看看beans.xml中ref元素的用法。
首先定义两个bean:helloBean、dateBean,分别指向HelloBean类和Date类。然后定义了hd1、hd2、hd3等三个bean,都指向HelloDate类。

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Eclipse插件合集

下一篇:Lesson 1.02 Ant的安装和配置