Spring之自动装配

4 篇文章 0 订阅
Spring由名称自动装配

Spring有一下几种装配模式:

  • 默认情况下,通过ref指定装配bean
  • byName - 根据属性名称自动装配
  • byType - 根据数据类型自动装配

还有两种我不怎么理解,以后再说

那么接下来分别说说这几种自动装配

默认设置

新建两个bean

package com.assembly.beans;

public class School {

	private String name = "Changchun college of architecture";

	public String getName() {
		return name;
	}

}
package com.assembly.beans;

public class Student {

	private School school;

	public School getSchool() {
		return school;
	}

	public void setSchool(School school) {
		this.school = school;
	}

}

默认设置下,我们通过’ref’属性来连接bean

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring-Common.xml -->
<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-2.5.xsd">

	<bean id="student" class="com.assembly.beans.Student">
		<property name="school" ref="school" />
	</bean>

	<bean id="school" class="com.assembly.beans.School"></bean>
</beans>
通过属性名称自动装配

按属性名称自动装配,就是指如果一个bean的名称与其他bean属性的名称一样,那么将自动装配它。以下面的xml文件为例:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring-Common.xml -->
<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-2.5.xsd">

	<bean id="student" class="com.assembly.beans.Student" autowire="byName"></bean>

	<bean id="school" class="com.assembly.beans.School">
		<property name="name" value="Changchun college of architecture"></property>
	</bean>
</beans>

如果 com.assembly.beans.Student 类中有个属性名称为 ‘school’,那么第二个bean将自动装配到第一个bean中。

package com.assembly.beans;

public class School {

	private String name;

	//...
}
package com.assembly.beans;

public class Student {

	private School school;

	//...
}

运行程序

在这里插入图片描述

但是如果 Student 类中的属性方法名为 ‘SCHOOL’,那么将会装配失败

package com.assembly.beans;

public class Student {

	private School SCHOOL;

	//...
}
通过属性类型自动装配

按属性类型自动装配是指一个bean的数据类型与其他bean属性的类型相同,则自动装配它。

我们以上面 通过属性名称自动装配 的例子来说明

我们把 School 类做一下修改,把变量 name 的数据类型改成布尔类型

package com.assembly.beans;

public class School {

	private Boolean name;

	public Boolean getName() {
		return name;
	}

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

修改XML文件,让Spring按照类型自动装配

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring-Common.xml -->
<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-2.5.xsd">

	<bean id="student" class="com.assembly.beans.Student" autowire="byType"></bean>

	<bean id="school" class="com.assembly.beans.School">
		<property name="name" value="Changchun college of architecture!!!"></property>
	</bean>

</beans>

再次运行程序,会报如下的错误

...
Caused by: java.lang.IllegalArgumentException: Invalid boolean value [Changchun college of architecture!!!]
	at org.springframework.beans.propertyeditors.CustomBooleanEditor.setAsText(CustomBooleanEditor.java:154)
	at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:466)
	at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:439)
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:192)
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:585)
	... 28 more

还有通过注解实现自动装配等的例子,后续会再做补充

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值