Spring Beans 自动装配

       Spring 的自动装配功能可以让 Spring 容器依据某种规则(自动装配的规则,有五种),为指定的 Bean 从应用的上下文(AppplicationContext 容器)中查找它所依赖的 Bean,并自动建立 Bean 之间的依赖关系。而这一过程是在完全不使用任何 <constructor-arg>和 <property> 元素 ref 属性的情况下进行的。Spring 的自动装配功能能够有效地简化 Spring 应用的 XML 配置,因此在配置数量相当多时采用自动装配降低工作量。

自动装配模式的规则

可用于指示 Spring 容器为来使用自动装配进行依赖注入。使用<bean>元素的 autowire 属性为一个 bean 定义指定自动装配模式。

属性值

说明

byName

按名称自动装配。

Spring 会根据的 Java 类中对象属性的名称,在整个应用的上下文 ApplicationContext(IoC 容器)中查找。若某个 Bean 的 id 或 name 属性值与这个对象属性的名称相同,则获取这个 Bean,并与当前的 Java 类 Bean 建立关联关系。

byType

按类型自动装配。

Spring 会根据 Java 类中的对象属性的类型,在整个应用的上下文 ApplicationContext(IoC 容器)中查找。若某个 Bean 的 class 属性值与这个对象属性的类型相匹配,则获取这个 Bean,并与当前的 Java 类的 Bean 建立关联关系。

constructor

与 byType 模式相似,不同之处在与它应用于构造器参数(依赖项),如果在容器中没有找到与构造器参数类型一致的 Bean,那么将抛出异常。

default

表示默认采用上一级元素 <beans> 设置的自动装配规则(default-autowire)进行装配。

no

默认值,表示不使用自动装配,Bean 的依赖关系必须通过 <constructor-arg>和 <property> 元素的 ref 属性来定义。

以使用 byType 或者 constructor 自动装配模式来连接数组和其他类型的集合。

自动装配的局限性

当自动装配始终在同一个项目中使用时,它的效果最好。如果通常不使用自动装配,它可能会使开发人员混淆的使用它来连接只有一个或两个 bean 定义。不过,自动装配可以显著减少需要指定的属性或构造器参数,但你应该在使用它们之前考虑到自动装配的局限性和缺点。

限制

描述

重写的可能性

你可以使用总是重写自动装配的 <constructor-arg>和 <property> 设置来指定依赖关系。

原始数据类型

你不能自动装配所谓的简单类型包括基本类型,字符串和类。

混乱的本质

自动装配不如显式装配精确,所以如果可能的话尽可能使用显式装配。

Spring 自动装配 byName

这种模式由属性名称指定自动装配。Spring 容器看作 beans,在 XML 配置文件中 beans 的 auto-wire 属性设置为 byName。然后,它尝试将它的属性与配置文件中定义为相同名称的 beans 进行匹配和连接。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常。

​
<beanid="dept"class="net.biancheng.c.Dept">
<propertyname="deptNo"value="1"></property>
<propertyname="deptName"value="技术部"></property>
</bean>
<beanid="employee"class="net.biancheng.c.Employee"autowire="byName">
<propertyname="empNo"value="002"></property>
<propertyname="empName"value="小郭"></property>
</bean>
</beans>
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
<beansxmlns="http://www.springframework.org/schema/beans"
​

Spring 自动装配 byType

这种模式由属性类型指定自动装配。Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 byType。然后,如果它的 type 恰好与配置文件中 beans 名称中的一个相匹配,它将尝试匹配和连接它的属性。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常。

​
<beanid="dept2"class="net.biancheng.c.Dept">
<propertyname="deptNo"value="1"></property>
<propertyname="deptName"value="技术部"></property>
</bean>
<beanid="employee"class="net.biancheng.c.Employee"autowire="byType">
<propertyname="empNo"value="002"></property>
<propertyname="empName"value="小郭"></property>
</bean>
</beans>
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
<beansxmlns="http://www.springframework.org/schema/beans"
​

 Spring 由构造函数自动装配

这种模式与 byType 非常相似,但它应用于构造器参数。Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 constructor。然后,它尝试把它的构造函数的参数与配置文件中 beans 名称中的一个进行匹配和连线。如果找到匹配项,它会注入这些 bean,否则,它会抛出异常。

​
<beanid="dept2"class="net.biancheng.c.Dept">
<constructor-argname="deptNo"value="1"></constructor-arg>
<constructor-argname="deptName"value="技术部"></constructor-arg>
</bean>
<beanid="employee"class="net.biancheng.c.Employee"autowire="constructor">
<constructor-argname="empNo"value="002"></constructor-arg>
<constructor-argname="empName"value="小郭"></constructor-arg>
</bean>
</beans>
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
<beansxmlns="http://www.springframework.org/schema/beans"
​

默认的自动装配模式(autowire="default")

默认采用上一级标签 <beans> 设置的自动装配规则(default-autowire)进行装配。

​
<beanid="dept2"class="net.biancheng.c.Dept">
<propertyname="deptNo"value="1"></property>
<propertyname="deptName"value="技术部"></property>
</bean>
<beanid="employee"class="net.biancheng.c.Employee"autowire="default">
<propertyname="empNo"value="002"></property>
<propertyname="empName"value="小郭"></property>
</bean>
</beans>
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"default-autowire="byType">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
<beansxmlns="http://www.springframework.org/schema/beans"
​

 

不使用自动装配(autowire="no")

autowire="no" 表示不使用自动装配,此时我们必须通过 <bean> 元素的 <constructor-arg>和 <property> 元素的 ref 属性维护 Bean 的依赖关系。

​
<beanid="dept"class="net.biancheng.c.Dept">
<propertyname="deptNo"value="1"></property>
<propertyname="deptName"value="技术部"></property>
</bean>
<beanid="employee"class="net.biancheng.c.Employee"autowire="no">
<propertyname="empNo"value="002"></property>
<propertyname="empName"value="小郭"></property>
<propertyname="dept"ref="dept"></property>
</bean>
</beans>
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"default-autowire="constructor">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
<beansxmlns="http://www.springframework.org/schema/beans"
​

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值