为什么Spring推荐使用构造器注入

Spring考试试卷_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Spring考试试卷
上传于|0|0|文档简介
&&Spring考试复习资料,JavaEE企业级开发复习考试资料。
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩7页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(1695)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'spring学习笔记一(01)(ioc的set和构造器注入)',
blogAbstract:'& 第一章程祖红:主讲
目标:1.理解Ioc 、 Aop2.掌握spring 对 业务层、持久层、表现层的编程支持。3.课下:关注一些spring的相关技术支持(比如说:任务调度、邮件、消息、安全、web服务、工作流)推荐书籍:《精通spring2.x企业级开发》---陈雄华
===============================================&&&&&&&&&&&&&&&& Spring内容:1.Spring概述:2.Spring核心(Ioc、Aop)3.Spring对持久层的支持(重点)',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:8,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:true,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}spring心得5--构造器注入@设置控制@案例加注解剖析 - 推酷
spring心得5--构造器注入@设置控制@案例加注解剖析
设置引用空对象的情况
设置null:
& &property name=&barlist&&
&&&&&&&& &null/&
& &/property&
& &&这种设置多出在一个bean实例引用另一个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=&memberBean& class=&www.csdn.spring.constructor.MemberBean&&
&!-- value=&null& 这里给value赋的是一个空字符串,而不是一个null空值 --&
&property name=&name& value=&null&/&
&property name=&member&&
&/property&
package www.csdn.spring.
import org.junit.T
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
public class TestMemberBean {
public void testBean(){
ApplicationContext context = new ClassPathXmlApplicationContext(&spring-null.xml&);
MemberBean member = context.getBean(&memberBean&,MemberBean.class);
System.out.println(member.name);
System.out.println(member.member);
构造器注入
&& &构造器注入是Set注入的替代;set注入是一种直接方式
它假设了所有的可变属性都可以通过set
方法访问到。例如有些属性在创建时设置一次,以后不再改变。
替代方式是通过构造函数设置一些属性值
注入的缺点是无法清晰表达哪些属性是必须的,哪些是可选的,构造注入的优势是通过构造强制依赖关系,不可能实例化不完全的或无法使用的bean
解决构造函数参数不确定性
&& 构造函数多或参数类型大都相同该如何处理?
&& &spring并不是按照参数的顺序来配置参数的。有三种方法来解决构造参数的不确定性:序号和类型,属性名字指明。&constructor-arg&有一个可选的index属性,可用来指定参数的顺序。
构造器案例剖析
&?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&&
&!-- 1、通过构造函数注入,采用默认配备方式注入值,这是最原始的方式 ,已由以下三种所代替 ;这里spring容器会去自动为你去匹配,但是会出现类型不匹配的错--&
&bean id=&employeeBean& class=&www.csdn.spring.constructor.EmployeeBean&&
&constructor-arg value=&杨凯& /&
&constructor-arg value=&男& /&
&constructor-arg value=&5000& /&
&constructor-arg ref=&deptmentBean& /&
&!-- 1、根据构造器参数的类型 ;这里需要注意的是类类型的需要加上包名指明,基本类型的不需要;特殊的注意的是这里不会自动拆箱装箱;bean实例使用的是基本类型这里就用基本类型注入,
使用额是基本类型对应的类类型就用加包名的类类型指明 --&
&!-- &bean id=&employeeBean& class=&www.csdn.spring.constructor.EmployeeBean&&
&constructor-arg type=&java.lang.String& value=&杨凯& /&
&constructor-arg type=&java.lang.String& value=&男& /&
&constructor-arg type=&double& value=&5000.00& /&
&constructor-arg type=&www.csdn.spring.constructor.DeptmentBean& ref=&deptmentBean& /&
&/bean& --&
&!-- 3、根据索引index注入 --&
&!-- &bean id=&employeeBean& class=&www.csdn.spring.constructor.EmployeeBean&&
&constructor-arg index=&0& value=&杨凯df&/&
&constructor-arg index=&1& value=&男&/&
&constructor-arg index=&2& value=&1000.00&/&
&constructor-arg index=&3& ref=&deptmentBean&/&
&/bean& --&
&!-- 4.根据属性名注入值,开发中最常用的一种构造器注入模式 --&
&!-- &bean id=&employeeBean& class=&www.csdn.spring.constructor.EmployeeBean&&
&constructor-arg name=&name& value=&杨凯& /&
&constructor-arg name=&sex& value=&男& /&
&constructor-arg name=&salary& value=&5000.00& /&
&constructor-arg name=&dept& ref=&deptmentBean& /&
&/bean& --&
&bean id=&deptmentBean& class=&www.csdn.spring.constructor.DeptmentBean&&
&property name=&name& value=&开发部& /&
&property name=&deptNo& value=&001& /&
package www.csdn.spring.
publicclass EmployeeBean {
private DeptmentB
public EmployeeBean(String name, String sex, double salary,
DeptmentBean dept) {
this.name =
this.sex =
this.salary =
this.dept =
public String toString() {
return&EmployeeBean [name=& + name + &, sex=& + sex + &, salary=&
+ salary + &, dept=& + dept + &]&;
辅助bean实例
package www.csdn.spring.
publicclass DeptmentBean {
private Integer deptNo;
publicvoid setName(String name) {
this.name =
publicvoid setDeptNo(Integer deptNo) {
this.deptNo = deptNo;
public String toString() {
return&deptment [name=& + name + &, deptNo=& + deptNo + &]&;
package www.csdn.spring.
import org.junit.T
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
public class TestEmployeeBean {
public void testBean(){
ApplicationContext context = new ClassPathXmlApplicationContext(&spring-constructor.xml&);
System.out.println(context.getBean(&employeeBean&,EmployeeBean.class).toString());
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致}

我要回帖

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信