您的位置:首页 > 编程语言 > Java开发

spring中用ref属性指定的三种方法

2015-12-14 20:25 567 查看
package javamxj.spring.basic.ref;

public class HelloBean {

private String hello;

public String getHello() {

return hello;

}

public void setHello(String hello) {

this.hello = hello;

}

}

package javamxj.spring.basic.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());

}

}

<?xml version="1.0" encoding="GBK"?>

<!DOCTYPE beans public "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="helloBean" class="javamxj.spring.basic.ref.HelloBean">

<property name="hello" value="Hello! Child Bean." />

</bean>

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

1.用bean属性指定(可以是id也可以是name)

<bean id="hd1" class="javamxj.spring.basic.ref.HelloDate">

<property name="hb">

<ref bean="helloBeanParent"/>

</property>

<property name="date">

<ref bean="#date"/>

<!--<ref bean="dateBean"/>-->

</property>

</bean>

2.用local指定(必须与id保持一致)

<bean id="hd2" class="javamxj.spring.basic.ref.HelloDate">

<property name="hb">

<ref local="helloBean"/>

</property>

<property name="date">

<ref local="dateBean"/>

</property>

</bean>

3.用parent属性指定

<bean id="hd3" class="javamxj.spring.basic.ref.HelloDate">

<property name="hb">

<ref parent="helloBean"/>

</property>

<property name="date">

<bean class="java.util.Date"/>

</property>

</bean>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: