04-spring的依赖注入
目錄
- 一、spring 中的依賴注入
- 二、構(gòu)造函數(shù)注入
- 1.實(shí)現(xiàn)類的構(gòu)造函數(shù) AccountServiceImpl
- 2.bean.xml
- 三、set 方法注入(更常用)
- 1.實(shí)現(xiàn)類 AccountServiceImpl2
- 2. bean.xml
- 四、復(fù)雜類型的注入/集合類型的注入
- 1.AccountServiceImpl3
- 2.bean.xml
一、spring 中的依賴注入
- Dependency Injection
- 降低程序間的耦合(依賴關(guān)系)
- 以后都交給spring來維護(hù)
- 在當(dāng)前類需要用到其他類的對象,由spring為我們提供,我們只需要在配置文件中說明
- 被稱之為依賴注入
- 能注入的數(shù)據(jù):有三類
- 基本類型和 String
- 其他 bean 類型(在配置文件中或者注解配置過的bean)
- 復(fù)雜類型/集合類型
二、構(gòu)造函數(shù)注入
一般不用
適應(yīng)的標(biāo)簽:constructor-arg
標(biāo)簽出現(xiàn)的位置:bean標(biāo)簽的內(nèi)部
標(biāo)簽中的屬性:
? type: 用于指定要注入的數(shù)據(jù)的數(shù)據(jù)類型,該數(shù)據(jù)類型也是構(gòu)造函數(shù)中某個(gè)或某些參數(shù)的類型。
? index: 用于指定要注入的數(shù)據(jù)給定構(gòu)造函數(shù)中指定索引位置的參數(shù)賦值。參數(shù)索引的位置從零開始。
? name:用于指定給構(gòu)造參數(shù)中指定名稱的參數(shù)賦值。
===============================以上三個(gè)用于指定給構(gòu)造函數(shù)中那個(gè)參數(shù)賦值==========================
? value: 用于提供基本類型和String類型的數(shù)據(jù)
? ref: 用于指定其他的 bean 類型數(shù)據(jù)。它指的就是在 spring 中 ioc 核心容器中出現(xiàn)過的 bean 對象
優(yōu)勢:
? 在獲取 bean 對象時(shí),注入數(shù)據(jù)是必須的操作,否則對象無法創(chuàng)建成功。
弊端:
? 改變了 bean 對象的實(shí)例化方式,使我們在創(chuàng)建對象時(shí),如果用不到這些數(shù)據(jù),也必須提供
1.實(shí)現(xiàn)類的構(gòu)造函數(shù) AccountServiceImpl
public class AccountServiceImpl implements IAccountService {//如果是經(jīng)常變化的數(shù)據(jù),并不適用于注入的方式private String name;private Integer age;private Date birthday;public AccountServiceImpl(String name, Integer age, Date birthday) {this.name = name;this.age = age;this.birthday = birthday;}public void saveChange() {System.out.println("保存了"+name+","+age+","+birthday);} }2.bean.xml
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="accountService" class="com.service.impl.AccountServiceImpl"><constructor-arg type="java.lang.String" name="name" value="test"/><constructor-arg name="age" value="18"/><constructor-arg name="birthday" ref="now"/></bean><!--配置一個(gè)日期對象--><bean id="now" class="java.util.Date"/> </beans>三、set 方法注入(更常用)
set 方法更加常用
涉及的標(biāo)簽:property
出現(xiàn)的位置:bean 標(biāo)簽的內(nèi)部
標(biāo)簽的屬性:
? name:用于指定給構(gòu)造參數(shù)中指定名稱的參數(shù)賦值。
? 注意:這里的name并不是屬性的名稱,而是 setName 方法中的 Name
? value: 用于提供基本類型和String類型的數(shù)據(jù)
? ref: 用于指定其他的 bean 類型數(shù)據(jù)。它指的就是在 spring 中 ioc 核心容器中出現(xiàn)過的 bean 對象
優(yōu)勢:
? 創(chuàng)建對象時(shí)沒有明確的限制,可以使用默認(rèn)構(gòu)造函數(shù)
弊端:
? 如果有某個(gè)成員必須有值,則獲取對象有可能 set 方法沒有執(zhí)行
1.實(shí)現(xiàn)類 AccountServiceImpl2
public class AccountServiceImpl2 implements IAccountService {private String name;private Integer age;private Date birthday;public void setName(String name) {this.name = name;}public void setAge(Integer age) {this.age = age;}public void setBirthday(Date birthday) {this.birthday = birthday;}public void saveChange() {System.out.println("保存了"+name+","+age+","+birthday);} }2. bean.xml
<bean id="accountService2" class="com.service.impl.AccountServiceImpl2"><property name="name" value="卡茲克"/><property name="age" value="19"/><property name="birthday" ref="now"/></bean>四、復(fù)雜類型的注入/集合類型的注入
用于給 List 結(jié)構(gòu)集合注入的標(biāo)簽:
- ·List Array set
- Map props
1.AccountServiceImpl3
public class AccountServiceImpl3 implements IAccountService {private String[] myStrings;private List<String> myList;private Set<String> mySet;private Map<String,String> myMap;private Properties myProps;public void setMyStrings(String[] myStrings) {this.myStrings = myStrings;}public void setMyList(List<String> myList) {this.myList = myList;}public void setMySet(Set<String> mySet) {this.mySet = mySet;}public void setMyMap(Map<String, String> myMap) {this.myMap = myMap;}public void setMyProps(Properties myProps) {this.myProps = myProps;}public void saveChange() {System.out.println(Arrays.toString(myStrings));System.out.println(myList);System.out.println(mySet);System.out.println(myMap);System.out.println(myProps);} }2.bean.xml
<bean id="accountService2" class="com.service.impl.AccountServiceImpl2"><property name="name" value="卡茲克"/><property name="age" value="19"/><property name="birthday" ref="now"/></bean><bean id="accountService3" class="com.service.impl.AccountServiceImpl3"><property name="myStrings"><array><value>aaa</value><value>bbb</value><value>ccc</value></array></property><property name="myList"><list><value>aaa</value><value>bbb</value><value>ccc</value></list></property><property name="mySet"><set><value>aaa</value><value>bbb</value><value>ccc</value></set></property><property name="myMap"><map><entry key="testA" value="aaa"/><entry key="testB" value="bbb"/><entry key="testC"><value>ccc</value></entry></map></property><property name="myProps"><props><prop key="testA">aaa</prop><prop key="testB">bbb</prop></props></property></bean>轉(zhuǎn)載于:https://www.cnblogs.com/zuiren/p/11415417.html
總結(jié)
以上是生活随笔為你收集整理的04-spring的依赖注入的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 02-耦合和解耦
- 下一篇: 03-spring bean