日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring心得5--构造器注入@设置控制@案例加注解剖析

發布時間:2025/5/22 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring心得5--构造器注入@设置控制@案例加注解剖析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.設置引用空對象的情況

設置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/beanshttp://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"><null/></property></bean></beans>測試類package www.csdn.spring.constructor;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestMemberBean {@Testpublic 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);}}


?

2.構造器注入

?? ?構造器注入是Set注入的替代;set注入是一種直接方式缺點是它假設了所有的可變屬性都可以通過set方法訪問到。例如有些屬性在創建時設置一次,以后不再改變。替代方式是通過構造函數設置一些屬性值

?? ?set注入的缺點是無法清晰表達哪些屬性是必須的,哪些是可選的,構造注入的優勢是通過構造強制依賴關系,不可能實例化不完全的或無法使用的bean

解決構造函數參數不確定性

??? 構造函數多或參數類型大都相同該如何處理?

?? ?spring并不是按照參數的順序來配置參數的。有三種方法來解決構造參數的不確定性:序號和類型,屬性名字指明。<constructor-arg>有一個可選的index屬性,可用來指定參數的順序。

3.構造器案例剖析

配置文件

<?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/beanshttp://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" /></bean><!-- 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" /></bean></beans>bean實例package www.csdn.spring.constructor;publicclass EmployeeBean {private String name;private String sex;privatedoublesalary;private DeptmentBean dept;public EmployeeBean(String name, String sex, double salary,DeptmentBean dept) {super();this.name = name;this.sex = sex;this.salary = salary;this.dept = dept;}@Overridepublic String toString() {return"EmployeeBean [name=" + name + ", sex=" + sex + ", salary="+ salary + ", dept=" + dept + "]";}}輔助bean實例package www.csdn.spring.constructor;publicclass DeptmentBean {private String name;private Integer deptNo;publicvoid setName(String name) {this.name = name;}publicvoid setDeptNo(Integer deptNo) {this.deptNo = deptNo;}@Overridepublic String toString() {return"deptment [name=" + name + ", deptNo=" + deptNo + "]";}}測試類package www.csdn.spring.constructor;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestEmployeeBean {@Testpublic void testBean(){ApplicationContext context = new ClassPathXmlApplicationContext("spring-constructor.xml");System.out.println(context.getBean("employeeBean",EmployeeBean.class).toString());}}


?

轉載于:https://www.cnblogs.com/yangkai-cn/archive/2013/04/25/4016891.html

總結

以上是生活随笔為你收集整理的spring心得5--构造器注入@设置控制@案例加注解剖析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。