概述
在Spring中,“类型自动装配”的意思是如果一个bean的数据类型与其它bean属性的数据类型相同,将自动兼容装配它。
例如,一个“persion” bean 公开以“ability”类数据类型作为属性,Spring会找到ability类相同的数据类型,并自动装配它的Bean。如果没有匹配找到,它什么也不做。
1. Beans
两个Bean,person 和 ability.
public class Person {
private Ability ability;
public Ability getAbility() {
return ability;
}
public void setAbility(Ability ability) {
this.ability = ability;
}
@Override
public String toString() {
return "Person [ability=" + ability + "]";
}
}
public class Ability {
private String skill;
public String getSkill() {
return skill;
}
public void setSkill(String skill) {
this.skill = skill;
}
@Override
public String toString() {
return "Ability [skill=" + skill + "]";
}
}
2. Spring Wiring
通常情况下,明确地装配 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="com.ray.common.Person">
<property name="ability" ref="invisible"/>
</bean>
<bean id="invisible" class="com.ray.common.Ability">
<property name="skill" value="Invisible"/>
</bean>
</beans>
输出
Person [ability=Ability [skill=Invisible]]
随着自动装配按类型启用后,可以保留ability属性未设置。Spring会发现相同的数据类型并自动装配它。
<?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">
<bean id="person" class="com.ray.common.Person" autowire="byType"/>
<bean id="invisible" class="com.ray.common.Ability">
<property name="skill" value="Invisible"/>
</bean>
</beans>
输出
Person [ability=Ability [skill=Invisible]]
如果你有两个Bean,都是类“ability”相同的数据类型?
<?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">
<bean id="person" class="com.ray.common.Person" autowire="byType"/>
<bean id="invisible" class="com.ray.common.Ability">
<property name="skill" value="Invisible"/>
</bean>
<bean id="steal" class="com.ray.common.Ability">
<property name="skill" value="Invisible"/>
</bean>
</beans>
输出
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'person' defined in class path resource [beans.xml]: Unsatisfied dependency expressed through bean property 'ability'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.ray.common.Ability' available: expected single matching bean but found 2: invisible,steal
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1439)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1326)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at com.ray.test.Test.main(Test.java:12)
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.ray.common.Ability' available: expected single matching bean but found 2: invisible,steal
at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:215)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1424)
... 13 more
在这种情况下,它会打出 UnsatisfiedDependencyException 的错误消息。
注意:
在类型的自动装配模式,就必须确保只有Bean 只有一个唯一的数据类型声明。
最后
以上就是失眠御姐为你收集整理的Spring由类型(Type)自动装配的全部内容,希望文章能够帮你解决Spring由类型(Type)自动装配所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复