av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Spring入門配置和DL依賴注入實現(xiàn)圖解

瀏覽:128日期:2023-08-07 16:56:46

1、Spring入門配置

1.1、起別名

給項目起別名

Spring入門配置和DL依賴注入實現(xiàn)圖解!

1.2、import

導(dǎo)入其他xml

Spring入門配置和DL依賴注入實現(xiàn)圖解

1.3、Bean的配置最重要的,又很多配置,我們先學(xué)一點

Spring入門配置和DL依賴注入實現(xiàn)圖解

2、依賴注入(DL)

很重要

2.1、set注入

三種方式:

<?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 https://www.springframework.org/schema/beans/spring-beans.xsd'><bean class='bing.pojo.Student'> <!-- Set注入--> <property name='name' value='小冰'/><!-- 引用注入,address--> <property name='address' ref='address'/><!-- 數(shù)組注入--> <property name='advantages'> <array> <value>帥</value> <value>情商高</value> <value>智慧</value> <value>沉穩(wěn)</value> <value>有錢</value> </array> </property><!-- set--> <property name='course'> <set> <value>賺錢</value> <value>情商</value> <value>心理學(xué)</value> <value>經(jīng)濟(jì)學(xué)</value> <value>哲學(xué)</value> <value>英語</value> <value>數(shù)學(xué)</value> <value>計算機(jī)</value> </set> </property><!-- map注入--><property name='grades'> <map> <entry key='java' value='10000'/> <entry key='math' value='200'/> <entry key='English' value='300'/> <entry key='psychology' value='400'/> </map></property></bean> <bean class='bing.pojo.Address'> <property name='address' value='大亳州'/> </bean></beans>

記住這些類型的注入就行了

2.2、構(gòu)造器注入

<?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 https://www.springframework.org/schema/beans/spring-beans.xsd'><bean class='bing.User'><!-- 第一種方式:通過index獲取--> <constructor-arg index='0' value='bingbing'/></bean> <bean class='bing.Student'><!-- 方式二:通過key value--> <constructor-arg name='gender' value='小仙女'></constructor-arg> <constructor-arg name='age' value='19'/> </bean> <bean class='bing.Student'><!-- 方式三:通過 類型獲取,不推薦可能會出現(xiàn)歧義比如兩個String--><!-- 這里以及上面為什么使用全類名? 肯定用到了反射--> <constructor-arg type='java.lang.String' value='女'/> <constructor-arg type='int' value='18'/> </bean><bean class='bing.Teacher'><!-- 我們來試一下兩個String類型會發(fā)生什么情況--> <constructor-arg type='java.lang.String' value='girl'/> <constructor-arg type='java.lang.String' value='劉老師'/></bean><!-- 相當(dāng)于new對象,對象名為person,只有這一個對象--> <bean class='bing.Person'> </bean></beans>

注意:我們一般選用 key value注入,見名知意

2.3、拓展注入

為了簡化我們可以引入p/c命名空間

使用方式:

<?xml version='1.0' encoding='UTF-8'?><beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:p='http://www.springframework.org/schema/p' xmlns:c='http://www.springframework.org/schema/c' xsi:schemaLocation='http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd'><!-- p,命名空間,p本質(zhì)上是什么? 就是properties,可以給簡單的屬性命名,目的是為了簡化--><bean p:name='zhangsan' p:age='18'></bean> <!-- c命名空間,怎么用? 是給constructor 構(gòu)造器初始化的,這里就要求必須要有有參構(gòu)造--> <bean c:name='bingbing' c:age='19' p:age='20'/></beans>

注意點:

使用前要導(dǎo)入:

xmlns:p='http://www.springframework.org/schema/p'xmlns:c='http://www.springframework.org/schema/c'

類比:

p相當(dāng)于標(biāo)簽:properties 其實就是set注入,不過可以簡化簡單的操作

c詳單與 :constructor:就是用來給有參構(gòu)造器初始化的

2.4、bean標(biāo)簽作用域

bean是什么?就是一個對象,也就是類的實例

我們可以給他設(shè)置單例模式等等

Spring入門配置和DL依賴注入實現(xiàn)圖解

單例模式

<bean />

<!-- the following is equivalent, though redundant (singleton scope is the default) --><bean scope='singleton'/>

原型模式

<bean scope='prototype'/>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产黄视频在线播放 | 九九99久久 | 成人午夜在线 | 午夜精品久久久久久久久久久久 | 亚洲一区二区中文字幕 | 亚洲精品日韩在线观看 | 欧美成人h版在线观看 | 羞羞的视频免费看 | 亚洲第一成人影院 | 成人精品久久 | 亚洲欧洲一区 | 成人精品国产一区二区4080 | 色爱区综合| 国产日韩欧美一区二区 | 国产在线网址 | 99久久日韩精品免费热麻豆美女 | 美女视频. | 中文字幕在线视频一区二区三区 | 日日噜噜噜夜夜爽爽狠狠视频, | 日韩一区二区av | 999精彩视频 | 久久久久国产一区二区三区 | 亚洲美女视频 | 观看av| 综合国产在线 | 欧美精品一区二区三区在线四季 | 9999精品视频 | 久久精品一级 | 欧美精品啪啪 | 欧美性生活免费 | 亚洲国产精品成人久久久 | 69福利影院| 成人精品一区二区 | 国产日韩久久久久69影院 | 男女污网站 | 午夜激情免费 | 999久久久| 一级毛片在线播放 | 亚洲欧美v | 欧美一区在线看 | 欧美亚州 |