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());
}
记得快乐
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南