Spring之利用autowire属性进行自动装配

spring的自动属性装配,其实就是说,对于bean的属性,不用使用手工显示装配,可以让spring自己通过在xml文件中按照一定的规则查找相关的符合条件的bean,装配为bean的属性。


这样说起来有点绕口,直接上代码。


首先,写一个辅助类。

package com.cmm;

public class Phone {
	private String brand;
	private int price;
	@Override
	public String toString() {
		return "Phone [brand=" + brand + ", price=" + price + "]";
	}

}



package com.cmm;

public class Person {
	private Phone phone;
	

	@Override
	public String toString() {
		return "Person [phone=" + phone + "]";
	}
}


(一)通过类型进行自动装配。

<?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-4.3.xsd">
	
	<bean id="hw" class="com.cmm.Phone" p:brand="hw" p:price="3000">
	</bean>
	
	<!-- 使用autowire属性进行自动装配 -->
	<!-- 
		autowire:byType:表示,未显示装配的bean,按照byType的方式进行自动装配。
		byType:  表示按照bean的类型进行装配,也就是按照属性的类型。
		这里person只有一个属性,就是Phone phone.
	 -->
	<bean id="person" class="com.cmm.Person" autowire="byType"></bean>

</beans>


但是,需要注意两点。

第一:如果同一个xml中有两个或两个以上的同一类型的bean,就会装配失败,抛出异常。

第二:如果没有找到匹配的bean,则该属性会被设置为null。

<?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-4.3.xsd">
	
	
	<!-- 装配两个同一类型的bean -->
	<bean id="hw" class="com.cmm.Phone" p:brand="hw" p:price="3000"></bean>
	
	<bean id="oppo" class="com.cmm.Phone" p:brand="oppo" p:price="2700"></bean>
	
	<!-- 使用autowire属性进行自动装配 -->
	<!-- 
		autowire:byType:表示,未显示装配的bean,按照byType的方式进行自动装配。
		byType:  表示按照bean的类型进行装配,也就是按照属性的类型。
		这里person只有一个属性,就是Phone phone.
	 -->
	 
	<bean id="person" class="com.cmm.Person" autowire="byType"></bean>

</beans>
添加一个属性
package com.cmm;

public class Person {
	private Phone phone;
	private String name;
	public void setPhone(Phone phone) {
		this.phone = phone;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Person [phone=" + phone + ", name=" + name + "]";
	}

}

配置如下:

<?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-4.3.xsd">
	
	
	<!-- 装配两个同一类型的bean -->
	<bean id="hw" class="com.cmm.Phone" p:brand="hw" p:price="3000"></bean>
	
	<!-- 使用autowire属性进行自动装配 -->
	<!-- 
		autowire:byType:表示,未显示装配的bean,按照byType的方式进行自动装配。
		byType:  表示按照bean的类型进行装配,也就是按照属性的类型。
		这里person只有一个属性,就是Phone phone.
	 -->
	 
	<bean id="person" class="com.cmm.Person" autowire="byType"></bean>

</beans>


结果如下:




name属性为:null。


(二)通过属性名字进行装配。

<?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-4.3.xsd">
	
	
	<!-- id设置为Person的属性phone, -->
	<bean id="phone" class="com.cmm.Phone" p:brand="hw" p:price="3000"></bean>
	
	<!-- 使用autowire属性进行自动装配 -->
	<!-- 
		autowire:byType:表示,未显示装配的bean,按照byType的方式进行自动装配。
		byType:  表示按照bean的类型进行装配,也就是按照属性的类型。
		这里person只有一个属性,就是Phone phone.
	 -->
	 
	 <!-- Person 有setPhone的方法,使用byname方式进行自动装配 -->
	<bean id="person" class="com.cmm.Person"  p:name="Tom" autowire="byName"></bean>

</beans>

使用名字进行自动装配,实际上就是在xml中寻找与类属性同名的bean,如XXX,然后调用该类的setXXX方法来设置属性。


总结:自动装配,就是使用bean的autowire属性按照不同的方式,调用类的setter方法,设置bean属性。常用的:byType与byName。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值