spring学习笔记03-spring-DI-依赖注入详解(通过xml配置文件来配置依赖注入)
生活随笔
收集整理的這篇文章主要介紹了
spring学习笔记03-spring-DI-依赖注入详解(通过xml配置文件来配置依赖注入)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
spring學(xué)習(xí)筆記03-spring-DI-依賴注入詳解
1.概念
2.構(gòu)造函數(shù)注入
3.set方法注入
4.集合的注入
需要被注入的實(shí)體對(duì)象
package com.itheima.service.impl;import com.itheima.service.IAccountService;import java.util.Arrays; import java.util.List; import java.util.Properties; import java.util.Set; import java.util.Map;/*** 賬戶的業(yè)務(wù)層實(shí)現(xiàn)類*/ public class AccountServiceImpl3 implements IAccountService {private String[] myStrs;private List<String> myList;private Set<String> mySet;private Map<String,String> myMap;private Properties myProps;public void setMyStrs(String[] myStrs) {this.myStrs = myStrs;}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 saveAccount(){System.out.println(Arrays.toString(myStrs));System.out.println(myList);System.out.println(mySet);System.out.println(myMap);System.out.println(myProps);}}spring-DI-依賴注入對(duì)應(yīng)的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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- spring中的依賴注入依賴注入:Dependency InjectionIOC的作用:降低程序間的耦合(依賴關(guān)系)依賴關(guān)系的管理:以后都交給spring來(lái)維護(hù)在當(dāng)前類需要用到其他類的對(duì)象,由spring為我們提供,我們只需要在配置文件中說(shuō)明依賴關(guān)系的維護(hù):就稱之為依賴注入。依賴注入:能注入的數(shù)據(jù):有三類基本類型和String其他bean類型(在配置文件中或者注解配置過(guò)的bean)復(fù)雜類型/集合類型注入的方式:有三種第一種:使用構(gòu)造函數(shù)提供第二種:使用set方法提供第三種:使用注解提供(明天的內(nèi)容)--><!--構(gòu)造函數(shù)注入:使用的標(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ù)賦值。索引的位置是從0開(kāi)始name:用于指定給構(gòu)造函數(shù)中指定名稱的參數(shù)賦值 常用的=============以上三個(gè)用于指定給構(gòu)造函數(shù)中哪個(gè)參數(shù)賦值===============================value:用于提供基本類型和String類型的數(shù)據(jù)ref:用于指定其他的bean類型數(shù)據(jù)。它指的就是在spring的Ioc核心容器中出現(xiàn)過(guò)的bean對(duì)象優(yōu)勢(shì):在獲取bean對(duì)象時(shí),注入數(shù)據(jù)是必須的操作,否則對(duì)象無(wú)法創(chuàng)建成功。弊端:改變了bean對(duì)象的實(shí)例化方式,使我們?cè)趧?chuàng)建對(duì)象時(shí),如果用不到這些數(shù)據(jù),也必須提供。--><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"><constructor-arg name="name" value="泰斯特"></constructor-arg><constructor-arg name="age" value="18"></constructor-arg><constructor-arg name="birthday" ref="now"></constructor-arg></bean><!-- 配置一個(gè)日期對(duì)象 --><bean id="now" class="java.util.Date"></bean><!-- set方法注入 更常用的方式涉及的標(biāo)簽:property出現(xiàn)的位置:bean標(biāo)簽的內(nèi)部標(biāo)簽的屬性name:用于指定注入時(shí)所調(diào)用的set方法名稱value:用于提供基本類型和String類型的數(shù)據(jù)ref:用于指定其他的bean類型數(shù)據(jù)。它指的就是在spring的Ioc核心容器中出現(xiàn)過(guò)的bean對(duì)象優(yōu)勢(shì):創(chuàng)建對(duì)象時(shí)沒(méi)有明確的限制,可以直接使用默認(rèn)構(gòu)造函數(shù)弊端:如果有某個(gè)成員必須有值,則獲取對(duì)象是有可能set方法沒(méi)有執(zhí)行。--><bean id="accountService2" class="com.itheima.service.impl.AccountServiceImpl2"><property name="name" value="TEST" ></property><property name="age" value="21"></property><property name="birthday" ref="now"></property></bean><!-- 復(fù)雜類型的注入/集合類型的注入用于給List結(jié)構(gòu)集合注入的標(biāo)簽:list array set用于個(gè)Map結(jié)構(gòu)集合注入的標(biāo)簽:map props結(jié)構(gòu)相同,標(biāo)簽可以互換--><bean id="accountService3" class="com.itheima.service.impl.AccountServiceImpl3"><property name="myStrs"><set><value>AAA</value><value>BBB</value><value>CCC</value></set></property><property name="myList"><array><value>AAA</value><value>BBB</value><value>CCC</value></array></property><property name="mySet"><list><value>AAA</value><value>BBB</value><value>CCC</value></list></property><property name="myMap"><props><prop key="testC">ccc</prop><prop key="testD">ddd</prop></props></property><property name="myProps"><map><entry key="testA" value="aaa"></entry><entry key="testB"><value>BBB</value></entry></map></property></bean> </beans>總結(jié)
以上是生活随笔為你收集整理的spring学习笔记03-spring-DI-依赖注入详解(通过xml配置文件来配置依赖注入)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 地震
- 下一篇: 在Windows 7解决GAC错误