Spring之ref


---------------------------------/SpringHello2/src/ref/HelloBean.java
package ref;


public class HelloBean {
private String hello;

public String getHello() {
return hello;
}

public void setHello(String hello) {
this.hello = hello;
}
}

---------------------------------/SpringHello2/src/ref/HelloDate.java

package ref;

import java.util.Date;


public class HelloDate {
private Date date;

private HelloBean hb;

public void setDate(Date date) {
this.date = date;
}

public void setHb(HelloBean hb) {
this.hb = hb;
}

public void sayHello() {
System.out.println(hb.getHello() + " " + date.toLocaleString());
}


}

---------------------------------/SpringHello2/src/ref/Test.java

package ref;


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


public class Test {

public static void main(String[] args) {
BeanFactory parent = new XmlBeanFactory(new ClassPathResource(
"ref/parent.xml"));
BeanFactory child = new XmlBeanFactory(new ClassPathResource(
"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();
}
}



---------------------------------/SpringHello2/src/ref/beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean" class="ref.HelloBean">
<property name="hello" value="Hello! Child Bean." />
</bean>

<bean id="dateBean" name="#date" class="java.util.Date" />

<bean id="hd1" class="ref.HelloDate">
<property name="hb">
<ref bean="helloBeanParent" />
</property>
<property name="date">
<ref bean="#date" />

</property>
</bean>

<bean id="hd2" class="ref.HelloDate">
<property name="hb">
<ref local="helloBean" />
</property>
<property name="date">
<ref local="dateBean" />
</property>
</bean>

<bean id="hd3" class="ref.HelloDate">
<property name="hb">
<ref parent="helloBean" />
</property>
<property name="date">
<bean class="java.util.Date" />
</property>
</bean>

</beans>
---------------------------------/SpringHello2/src/ref/parent.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<bean id="helloBean" class="ref.HelloBean">
<property name="hello" value="Hello! ZYLJava." />
</bean>
<bean id="helloBeanParent" class="ref.HelloBean">
<property name="hello" value="Hello! Parent Bean." />
</bean>

</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值