spring的自动装配(default-autowire="byName") 与ref
spring为对象属性注入对象有二种方式 方法有 如下两种:自动装配(default-autowire)与ref (<property name="类的属性名" ref="要注入的对象名称"/>)
自动装配,官方给出的定义是这样:
Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系。因此,如果可能的话,可以自
动让Spring通过检查BeanFactory中的内容,来替我们指定bean的协作者(其他被依赖的bean)。由于
autowire可以针对单个bean进行设置,因此可以让有些bean使用autowire,有些bean不采用。autowire的
方便之处在减少或者消除属性或构造器参数的设置,这样可以给我们的配置文件减减肥
其实,自动装配就是让我们少些几个<property name="类的属性名" ref="要注入的对象名称"/>
实例:
1 public class StudentDao : AdoDaoSupport 2 { 3 public Student GetStudent() 4 { 5 return (Student)AdoTemplate.QueryForObject(CommandType.Text, "SELECT top 1 * FROM Student", new StudentMapper<Student>()); 6 } 7 }
上面类StudentDao继续了AdoDaoSupport,要使用AdoTemplate对象必须从外部注入AdoTemplate
第一种::通过 default-autowire="byName"
如下图:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <sectionGroup name="spring"> 5 <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> 6 <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> 7 </sectionGroup> 8 </configSections> 9 <spring> 10 <context> 11 <resource uri="config://spring/objects"/> 12 </context> 13 14 <!--错误 没有添加 default-autowire="byName"--> 15 <!--<objects xmlns="http://www.springframework.net" xmlns:db="http://www.springframework.net/database">--> 16 17 <!--正确 添加def ault-autowire="byName"--> 18 <objects xmlns="http://www.springframework.net" xmlns:db="http://www.springframework.net/database" default-autowire="byName"> 19 20 <!--配置DbProvider--> 21 <db:provider id="DbProvider" provider="System.Data.SqlClient" connectionString="server=.;database=Spring;integrated security=true;"/> 22 23 <!--配置AdoTemplate AdoTemplate依赖于DbProvider--> 24 <object id="adoTemplate" type="Spring.Data.Generic.AdoTemplate, Spring.Data"> 25 <property name="DbProvider" ref="DbProvider"/> 26 </object> 27 <!--注入StudentDao--> 28 <object id="StudentDao" type="Common.Dao.StudentDao,Common"></object> 29 </objects> 30 </spring> 31 </configuration>
第二种方式通过ref
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <sectionGroup name="spring"> 5 <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> 6 <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> 7 </sectionGroup> 8 </configSections> 9 <spring> 10 <context> 11 <resource uri="config://spring/objects"/> 12 </context> 13 14 <!--错误 没有添加 default-autowire="byName"--> 15 <!--<objects xmlns="http://www.springframework.net" xmlns:db="http://www.springframework.net/database">--> 16 17 <!--正确 添加def ault-autowire="byName"--> 18 <objects xmlns="http://www.springframework.net" xmlns:db="http://www.springframework.net/database"> 19 20 <!--配置DbProvider--> 21 <db:provider id="DbProvider" provider="System.Data.SqlClient" connectionString="server=.;database=Spring;integrated security=true;"/> 22 23 <!--配置AdoTemplate AdoTemplate依赖于DbProvider--> 24 <object id="adoTemplate" type="Spring.Data.Generic.AdoTemplate, Spring.Data"> 25 <property name="DbProvider" ref="DbProvider"/> 26 </object> 27 <!--注入StudentDao--> 28 <object id="StudentDao" type="Common.Dao.StudentDao,Common"> 29 <property name="AdoTemplate" ref="adoTemplate"/> 30 </object> 31 </objects> 32 </spring> 33 </configuration>

没有装配成功报错:
错误收集
001错误
提示 : Error creating context 'spring.root': DbProvider or AdoTemplate is required
原因:没有添加default-autowire="byName"

1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <sectionGroup name="spring"> 5 <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> 6 <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> 7 </sectionGroup> 8 </configSections> 9 <spring> 10 <context> 11 <resource uri="config://spring/objects"/> 12 </context> 13 14 <!--错误 没有添加 default-autowire="byName"--> 15 <!--<objects xmlns="http://www.springframework.net" xmlns:db="http://www.springframework.net/database">--> 16 17 <!--正确 添加def ault-autowire="byName"--> 18 <objects xmlns="http://www.springframework.net" xmlns:db="http://www.springframework.net/database" default-autowire="byName"> 19 20 <!--配置DbProvider--> 21 <db:provider id="DbProvider" provider="System.Data.SqlClient" connectionString="server=.;database=Spring;integrated security=true;"/> 22 23 <!--配置AdoTemplate AdoTemplate依赖于DbProvider--> 24 <object id="adoTemplate" type="Spring.Data.Generic.AdoTemplate, Spring.Data"> 25 <property name="DbProvider" ref="DbProvider"/> 26 </object> 27 28 <!--注入StudentDao--> 29 <object id="StudentDao" type="Common.Dao.StudentDao,Common"></object> 30 31 32 </objects> 33 </spring> 34 </configuration>
对应项目名称


浙公网安备 33010602011771号