欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring IOC (DI) 依賴(lài)注入的四種方式示例詳解

 更新時(shí)間:2023年06月26日 11:45:17   作者:清瀟和梨花  
這篇文章主要介紹了Spring IOC (DI) 依賴(lài)注入的四種方式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

依賴(lài)注入的四種方式:

set 注入

賦值,默認(rèn)使用的是set() 方法,依賴(lài)注入底層是通過(guò)反射實(shí)現(xiàn)的

<bean id="student" class="cust.zaw.entity.Student">
	<property name="stuName" value="lc"></property>
</bean>

構(gòu)造器注入

使用構(gòu)造方法注入

<bean id="student" class="cust.zaw.entity.Student">
參數(shù)順序相同的話,可以不寫(xiě)index
<constructor-arg value=""></constructor-arg>
參數(shù)順序不同的話,寫(xiě)index或者name或者type
<constructor-arg value="" index=“0”></constructor-arg>
<constructor-arg value="" index=“1”></constructor-arg>
<constructor-arg value="" name=“”></constructor-arg>
<constructor-arg value="" type=“”></constructor-arg>
</bean>

p命名空間注入

引入命名空間就是加一句話:xmlns:p=“http://www.springframework.org/schema/p”

也可以這樣引入,它會(huì)自動(dòng)引入命名空間

引入命名空間后,就可以這樣以這樣的方式實(shí)現(xiàn)依賴(lài)注入

<bean id="student" class="cust.zaw.entity.Student" p:stuAge="" p:stuName="" p:stuNO-ref="">
</bean>
  • 注意:

無(wú)論是String 還是int / short / long ,在賦值時(shí)都是value=“值”,因此建議 配合type / name 進(jìn)行區(qū)分

  • IOC 容器賦值:

簡(jiǎn)單類(lèi)型,value=“”對(duì)象類(lèi)型(當(dāng)一個(gè)類(lèi)中有其他類(lèi) 類(lèi)型,為其賦值時(shí)使用),ref=“需要引用的id值”

注入各種集合數(shù)據(jù)類(lèi)型

List Set Map properties

寫(xiě)一個(gè)實(shí)體類(lèi)

package cust.zaw.entity;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class AllCollectionType {
	private List<String> list;
	private String[] array;
	private Set<String> set;
	private Map<String,String> map;
	private Properties props;
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public String[] getArray() {
		return array;
	}
	public void setArray(String[] array) {
		this.array = array;
	}
	public Set<String> getSet() {
		return set;
	}
	public void setSet(Set<String> set) {
		this.set = set;
	}
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public Properties getProps() {
		return props;
	}
	public void setProps(Properties props) {
		this.props = props;
	}
	@Override
	public String toString() {
		String strContent=null;
		for(String str : array) {
			strContent +=str + ",";
		}
		return "AllCollectionType [getList()=" + getList() + ", getArray()=" + Arrays.toString(getArray())
				+ ", getSet()=" + getSet() + ", getMap()=" + getMap() + ", getProps()=" + getProps() + "]"+strContent;
	}
}

編寫(xiě)配置文件

<bean id="collectionDemo" class="cust.zaw.entity.AllCollectionType">
		<property name="list">
			<list>
				<value>足球</value>
				<value>籃球</value>
				<value>乒乓球</value>
			</list>
		</property>
		<property name="array">
			<array>
				<value>足球1</value>
				<value>籃球1</value>
				<value>乒乓球1</value>
			</array>
		</property>
		<property name="set">
			<set>
				<value>足球2</value>
				<value>籃球2</value>
				<value>乒乓球2</value>
			</set>
		</property>
		<property name="map">
			<map>
				<entry>
					<key>
						<value>football3</value>
					</key>
					<value>足球3</value>
				</entry>
				<entry>
					<key>
						<value>basketball3</value>
					</key>
					<value>籃球3</value>
				</entry>
				<entry>
					<key>
						<value>ppq3</value>
					</key>
					<value>乒乓球3</value>
				</entry>
			</map>
		</property>
		<property name="props">
			<props>
				<prop key="football4">足球4</prop>
				<prop key="baskball4">籃球4</prop>
				<prop key="pp4">乒乓球4</prop>
			</props>
		</property>		
	</bean>

獲取輸出

public static void collectionDemp() {
?? ??? ?ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
?? ??? ?AllCollectionType type=(AllCollectionType)context.getBean("collectionDemo");
?? ??? ?System.out.println(type);
?? ?}

給對(duì)象類(lèi)型賦值null:

<!-- 注意沒(méi)有<value> -->
<property name="name">
			<null/>
</property>

賦空值

<property name="name">
			<value></value>
</property>

自動(dòng)裝配注入(只適用于 ref 類(lèi)型):

約定優(yōu)于配置

  • byName:自動(dòng)尋找:其他bean的id值 = 該Course類(lèi)的屬性名
  • byType:其他類(lèi)型(class)是否與該Course類(lèi)的ref 屬性類(lèi)型一致
  • (必須滿足當(dāng)前ioc容器只有一個(gè)bean滿足)
  • constructor:其他bean的類(lèi)型(class)是否與該Course 類(lèi)的構(gòu)造方法參數(shù) 的類(lèi)型一致;
<!--
		autowire="byName"
		Course類(lèi)中有一個(gè)ref屬性teacher(屬性名),并且該ioc容器中恰好有一個(gè) bean的id也是teacher
		bean的id=類(lèi)的屬性名 
 -->
<bean id="student" class="cust.zaw.entity.Student" autowire="byName">
		<property name="stuNO" value="2"></property>
		<property name="stuName" value="lc"></property>
		<property name="stuAge" value="24"></property>
		<!-- <property name="teacher" ref="teacher"></property>-->
</bean>

可以在頭文件中,一次性將ioc容器的所有bean 統(tǒng)一設(shè)置成自動(dòng)裝配自動(dòng)裝配雖然可以減少代碼量,但是會(huì)降低程序可讀性

<beans xmlns="http://www.springframework.org/schema/beans"
	....
	default-autowire="byName">

到此這篇關(guān)于Spring IOC (DI) 依賴(lài)注入的四種方式的文章就介紹到這了,更多相關(guān)Spring IOC依賴(lài)注入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java基礎(chǔ)的詳細(xì)了解第四天

    java基礎(chǔ)的詳細(xì)了解第四天

    這篇文章對(duì)Java編程語(yǔ)言的基礎(chǔ)知識(shí)作了一個(gè)較為全面的匯總,在這里給大家分享一下。需要的朋友可以參考,希望能給你帶來(lái)幫助
    2021-08-08
  • Spring:@Async注解和AsyncResult與CompletableFuture使用問(wèn)題

    Spring:@Async注解和AsyncResult與CompletableFuture使用問(wèn)題

    這篇文章主要介紹了Spring:@Async注解和AsyncResult與CompletableFuture使用問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java類(lèi)的定義以及執(zhí)行順序?qū)W習(xí)教程

    Java類(lèi)的定義以及執(zhí)行順序?qū)W習(xí)教程

    這篇文章主要介紹了Java類(lèi)的定義以及執(zhí)行順序?qū)W習(xí)教程,包括對(duì)象的創(chuàng)建等面向?qū)ο缶幊痰幕A(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09
  • MyBatis緩存實(shí)現(xiàn)原理及代碼實(shí)例解析

    MyBatis緩存實(shí)現(xiàn)原理及代碼實(shí)例解析

    這篇文章主要介紹了MyBatis緩存實(shí)現(xiàn)原理及代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 分隔List集合,按指定大小,將集合分成多個(gè)的方法

    分隔List集合,按指定大小,將集合分成多個(gè)的方法

    下面小編就為大家?guī)?lái)一篇分隔List集合,按指定大小,將集合分成多個(gè)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • JDK8中新增的Optional工具類(lèi)基本使用

    JDK8中新增的Optional工具類(lèi)基本使用

    Optional不是對(duì)null關(guān)鍵字的一種替代,而是對(duì)于null判定提供了一種更加優(yōu)雅的實(shí)現(xiàn),接下來(lái)通過(guò)本文給大家分享JDK8中新增的Optional工具類(lèi)基本使用,感興趣的朋友跟隨小編一起看看吧
    2021-06-06
  • SpringBoot2新特性 自定義端點(diǎn)詳解

    SpringBoot2新特性 自定義端點(diǎn)詳解

    這篇文章主要介紹了SpringBoot2新特性 自定義端點(diǎn)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java?File類(lèi)提供的方法與操作

    Java?File類(lèi)提供的方法與操作

    Java使用File類(lèi)來(lái)表示計(jì)算機(jī)系統(tǒng)磁盤(pán)文件的對(duì)象類(lèi)型。File中提供了大量的方法,可以對(duì)文件進(jìn)行增加、刪除、修改、重命名等常規(guī)操作。File類(lèi)的對(duì)象會(huì)存儲(chǔ)文件自身的信息,例如文件在系統(tǒng)中的存儲(chǔ)目錄、文件大小、文件讀寫(xiě)權(quán)限等
    2023-03-03
  • 通過(guò)Spring層面進(jìn)行事務(wù)回滾的實(shí)現(xiàn)

    通過(guò)Spring層面進(jìn)行事務(wù)回滾的實(shí)現(xiàn)

    本文主要介紹了通過(guò)Spring層面進(jìn)行事務(wù)回滾的實(shí)現(xiàn),包括聲明式事務(wù)和編程式事務(wù),具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-04-04
  • 在Mybatis中association標(biāo)簽多層嵌套的問(wèn)題

    在Mybatis中association標(biāo)簽多層嵌套的問(wèn)題

    這篇文章主要介紹了在Mybatis中association標(biāo)簽多層嵌套的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評(píng)論