Spring中引用標簽ref和屬性ref


ref作為屬性
<bean id="students" class="com.yiwen.Students">
    <property name="group" ref="group">
</bean>

<bean id="group" class="com.yiwen.Group" />

ref作為標簽
local=”group” 表示在該xml文件中查找group(Spring4.0 沒有local屬性了,它的作用其實和ref屬性一樣,用屬性也行)

<bean id="students" class="com.yiwen.Students">
    <property name="group" >
        <ref local="group">
    </property>
</bean>

<bean id="group" class="com.yiwen.Group">

bean=”group” 表示全局查找id為group的bean,可在不同的xml中查找group

spring-main.xml

```
<bean id="students" class="com.yiwen.Students">
    <property name="group" >
        <ref bean="group">
    </property>
</bean>
```

spring-part.xml

<bean id="group" class="com.yiwen.Group">
1
這樣spring-main.xml中一樣可以找到spring-part.xml中的id為group的bean

使用ref標簽的實例
雖然直接使用ref屬性非常簡單方便,但是有時候xml文件存在多個,利用bean,不管在不在一個xml文件中都可以識別到。
編寫三個類School 和Students Teachers
Teachers

public class Teachers {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Students

public class Students {
    private String name ;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

School

public class School {
    private Students students;
    private Teachers teachers;

    public School(Students students, Teachers teachers) {
        this.students = students;
        this.teachers = teachers;
    }
    public Students getStudents() {
        return students;
    }

    public Teachers getTeachers() {
        return teachers;
    }
}

配置xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="school" class="com.yiwen.test6.School">
        <constructor-arg name="students">
            <ref bean="students"></ref>
        </constructor-arg>
        <constructor-arg name="teachers">
            <ref bean="teachers"></ref>
        </constructor-arg>
    </bean>
    <bean id="students" class="com.yiwen.test6.Students" >
        <property name="name">
            <value type="java.lang.String">yiwen</value>
        </property>
    </bean>
    <bean id="teachers" class="com.yiwen.test6.Teachers">
        <property name="name">
            <value type="java.lang.String">zhangsan</value>
        </property>
    </bean>
</beans>

測試類

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("test6.xml");
        School school=(School)context.getBean("school");
        System.out.print("student name:"+school.getStudents().getName()+"teacher name :"+school.getTeachers().getName());


    }
}

結果如下所示:

student name:yiwenteacher name :zhangsan
1
2
表明Students和Teacher的bean已經裝配到了School中了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM