zoukankan      html  css  js  c++  java
  • spring中ref标签的用法

    spring中ref标签的用法

    1、作用

    将对象值注入到对应的属性,依赖于配置标签实现

    2、属性

    • bean:通过该属性可以引用同一容器或父容器中的bean对象
    • parent: 引用父容器中的bean

    3、用法

    bean属性用法比较简单,这里就不再介绍,主要讲一下parent属性的用法

    前提

    • 一个Boss类中有name,Car属性,对应一个BossBean.xml配置文件
    • 一个Car类中有brand、price、color属性,对应一个CarBean.xml配置文件

    需求

    • 在BossBean.xml配置文件中引用CarBean.xml配置文件的bean

    Boos类

    package com.yl.bean;
    
    public class Boss {
        private String name;
        private Car car;
    
        public Boss() {
        }
    
        public Boss(String name, Car car) {
            this.name = name;
            this.car = car;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Car getCar() {
            return car;
        }
    
        public void setCar(Car car) {
            this.car = car;
        }
    }
    
    

    Car类

    package com.yl.bean;
    
    public class Car {
        private String brand;
        private Integer price;
        private String color;
    
        public Car() {
        }
    
        public Car(String brand, Integer price, String color) {
            this.brand = brand;
            this.price = price;
            this.color = color;
        }
    
        public String getBrand() {
            return brand;
        }
    
        public void setBrand(String brand) {
            this.brand = brand;
        }
    
        public Integer getPrice() {
            return price;
        }
    
        public void setPrice(Integer price) {
            this.price = price;
        }
    
        public String getColor() {
            return color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
    }
    
    

    BossBean.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">
    
        <!--Boss-->
        <bean id="boss" class="com.yl.bean.Boss">
            <property name="name" value="yl001"></property>
            
            <property name="car">
                <!--引用父容器中的car-->
                <ref parent="car"></ref>
            </property>
        </bean>
    
    </beans>
    

    CarBean.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">
    
        <!--Car-->
        <bean id="car" class="com.yl.bean.Car">
            <property name="brand" value="兰博基尼"></property>
            <property name="color" value="红色"></property>
            <property name="price" value="1000000"></property>
        </bean>
    
    </beans>
    

    4、测试类

     public static void main(String[] args) {
            //父容器 (CarBean)
            ApplicationContext parentCtx=new ClassPathXmlApplicationContext("CarBean.xml");
            //子容器(BoosBean)关联父容器
            ApplicationContext childCtx=new ClassPathXmlApplicationContext(new String[] {"BossBean.xml"},parentCtx);
    
            Boss boss= (Boss) childCtx.getBean("boss");
            System.out.println("老板名 ="+boss.getName());
            System.out.println("所用的车 ="+boss.getCar().getBrand());
        }
    
    记得快乐
  • 相关阅读:
    对于近期学习上的复习与整理
    ACM的奇计淫巧_输入挂
    hdu2602 DP (01背包)
    hdu 1723 DP/递推
    hdu1428 记忆化搜索(BFS预处理最短路径和+DP+DFS)
    hdu1355
    hdu1331&&hdu1579记忆化搜索(DP+DFS)
    hdu1257 dp(最长上升子序列)
    hdu1208 dp
    hdu 1203 dp(关于概率的```背包?)
  • 原文地址:https://www.cnblogs.com/Y-wee/p/13815014.html
Copyright © 2011-2022 走看看