【Spring】spring的自动装配(default-autowire="byName")
来源:互联网 发布:美国出口数据 编辑:程序博客网 时间:2024/05/22 03:36
自动装配,官方给出的定义是这样:
配置文件为.
x有4个选择:byName,byType,constructor和autodetect
1. byName:
xml中并没有给 bean service配Source属性,但在beans中设置了autowire="byName",这样配置文件会自动根据 cn.hh.spring.Service 中的setSource找bean id="Source"的bean ,然后自动配上去,如果没找到就不装配。
注意:byName的name是java中setXxxx 的Xxxx, 和上面设置的Source source中source拼写毫无关系,完全可以是
Service.java同上
applicationContext.xml
3. constructor:
试图在容器中寻找与需要自动装配的bean的构造函数参数一致的一个或多个bean,如果没找到则抛出异常。
4. autodetect:
首先尝试使用constructor来自动装配,然后再使用byType方式。
Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系。因此,如果可能的话,可以自动让Spring通过检查BeanFactory中的内容,来替我们指定bean的协作者(其他被依赖的bean)。由于autowire可以针对单个bean进行设置,因此可以让有些bean使用autowire,有些bean不采用。autowire的方便之处在减少或者消除属性或构造器参数的设置,这样可以给我们的配置文件减减肥其实,自动装配就是让我们少些几个 <ref ="...">.我们还是从需求出发.我们假设有4个bean.分别是bean2,bean3,bean4,bean5..其中,bean2里面有后面几个的引用..我只放出来bean2.java.
package com.test.model;public class Bean2 {private Bean3 bean3;private Bean4 bean4;private Bean5 bean5;public Bean3 getBean3(){return bean3;}public void setBean3(Bean3 bean3){this.bean3 = bean3;}public Bean4 getBean4(){return bean4;}public void setBean4(Bean4 bean4){this.bean4 = bean4;}public Bean5 getBean5(){return bean5;}public void setBean5(Bean5 bean5){this.bean5 = bean5;}}我们看不用自动装配的写法
<bean id="bean2" class="com.test.model.Bean2"><property name="bean3" ref="bean3"/><property name="bean4"><ref bean="bean4"/></property><property name="bean5" ref="bean5"/></bean>很明显,下面的几个属性设置是很繁琐的..我们假设使用自动装配.我们只需要这样
<bean id="bean2" class="com.test.model.Bean2" />里面的装配都不用写.当然,自动装配必须满足两点
(1)bean2.java里面的属性名字必须和applicationContext.xml里面对应的bean id的名字相同..也就是private Bean3 bean3; 这个bean3(其实对应的是get,set方法)必须和
<bean id="bean3" class="com.test.model.Bean3" parent="abstractBean"><property name="name" value="Tom" /><property name="password" value="123" /></bean>这个bean3相同.否则不能自动装配(2)在申明里配置一个属性.default-autowire="byName"(通过名字自动装配)
配置文件为.
<?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-2.0.xsd" default-autowire="byName"><bean id="abstractBean" abstract="true"><property name="id"><value>1000</value></property><property name="name" value="java" /></bean><bean id="bean2" class="com.test.model.Bean2" /><bean id="bean3" class="com.test.model.Bean3" parent="abstractBean"><property name="name" value="Tom" /><property name="password" value="123" /></bean><bean id="bean4" class="com.test.model.Bean4" parent="abstractBean" /><bean id="bean5" class="com.test.model.Bean5"><property name="age" value="20" /></bean></beans>default-autowire="x"
x有4个选择:byName,byType,constructor和autodetect
1. byName:
Service.javapublic class Service{ Source source; public void setSource(Source source) { this.source = source; }}applicationContext.xml
<beans ... default-autowire="byName"> <bean id="source" class="cn.hh.spring.DBCPSource" scope="prototype"/> <bean id="service" class="cn.hh.spring.Service" scope="prototype"></bean></beans>cn.hh.spring.DBCPSource实现了Source接口
xml中并没有给 bean service配Source属性,但在beans中设置了autowire="byName",这样配置文件会自动根据 cn.hh.spring.Service 中的setSource找bean id="Source"的bean ,然后自动配上去,如果没找到就不装配。
注意:byName的name是java中setXxxx 的Xxxx, 和上面设置的Source source中source拼写毫无关系,完全可以是
public class Service{ Source source1; public void setSource(Source source1){ this.source1 = source1; }}2. byType:
Service.java同上
applicationContext.xml
<beans ... default-autowire="byType"> <bean id="dbcpSource" class="cn.hh.spring.DBCPSource" scope="prototype"/> <bean id="service" class="cn.hh.spring.Service" scope="prototype"></bean></beans>同样没有配置setSource,autowire改成 "byType",配置文件会找实现了Source接口的bean,这里cn.hh.spring.DBCPSource 实现了Source接口,所以自动装配,如果没找到则不装配。如果同个配制文件中两个bean实现了Source接口,则报错。这里的 Type是指setSource(Source source)中参数的类型。
3. constructor:
试图在容器中寻找与需要自动装配的bean的构造函数参数一致的一个或多个bean,如果没找到则抛出异常。
4. autodetect:
首先尝试使用constructor来自动装配,然后再使用byType方式。
0 0
- 【Spring】spring的自动装配(default-autowire="byName")
- spring的自动装配(default-autowire="byName")
- spring的自动装配(default-autowire="byName")
- spring的自动装配(default-autowire="byName")
- spring的自动装配(default-autowire="byName")
- Spring的自动装配 default-autowire="byName"
- spring的自动装配(default-autowire="byName")
- spring的自动装配(default-autowire="byName")
- spring的自动装配(default-autowire="byName")
- spring的自动装配(default-autowire="byName")
- spring的default-lazy-init参数和spring的自动装配(default-autowire="byName")
- spring 自动装配 default-autowire="byName/byType"
- Spring中的自动装配(default-autowire="byName")
- spring 自动装配 default-autowire="byName/byType"
- spring 自动装配 default-autowire="byName/byType"
- Spring Autowire自动装配byName和byType
- spring 自动装配autowire=byName或byType
- spring中的default-autowire="byName"
- CLion中配置OpenCV环境问题
- Xcode定位crash代码位置
- HTML5之表单新功能 2.新的表单属性
- Android Html5 页面加载不出
- 楚楚街笔试题—航线
- 【Spring】spring的自动装配(default-autowire="byName")
- 如何Disable “Shut Down Event Tracker”?
- 线程的操作
- C++基础::为什么不能cout一个string?
- 通过poll修改最大并发数(the maximum concurrent connection improved by poll )
- Spring bean生命周期---ApplicationContext
- 2015(2016届)校园招聘季——百度 篇
- 四种方案解决:当ScollerView里面存在ABSListView的时候
- ubuntu 14.04 ifconfig 设置 static IP 无效,会自动刷新问题