spring中bean之间的关系配置

本文介绍了Spring框架中Bean的三种配置方式:继承配置、依赖配置和引用配置,并通过具体示例展示了如何实现这些配置。
bean之间的关系有如下几种配置
  • 继承
  • 依赖
  • 引用

继承配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="abstractIphone" class="com.yaspeed.spring.bean.cycle.Iphone7" 
		p:name="iphone7" p:price="7188" <span style="color:#FF0000;">abstract="true"</span>></bean>
	<bean id="iphone7" class="com.yaspeed.spring.bean.cycle.Iphone7" init-method="init"
		destroy-method="cleanUp" parent="abstractIphone">
	</bean>
	
</beans>

依赖配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="iphone7" class="com.yaspeed.spring.bean.cycle.Iphone7" 
		p:name="iphone7" p:price="7188" ></bean>
	<bean id="iphone7Plus" class="com.yaspeed.spring.bean.assembly.Iphone7Plus" 
		depends-on="iphone7">
	</bean>
	
</beans>

引用配置

<bean id="service" class="com.atguigu.spring.ref.Service">
	<!-- 通过 ref 属性值指定当前属性指向哪一个 bean! -->
	<property name="dao" ref="dao5"></property>		
</bean>








评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值