国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Spring入門配置和DL依賴注入實現圖解

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

1、Spring入門配置

1.1、起別名

給項目起別名

Spring入門配置和DL依賴注入實現圖解!

1.2、import

導入其他xml

Spring入門配置和DL依賴注入實現圖解

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

Spring入門配置和DL依賴注入實現圖解

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'/><!-- 數組注入--> <property name='advantages'> <array> <value>帥</value> <value>情商高</value> <value>智慧</value> <value>沉穩</value> <value>有錢</value> </array> </property><!-- set--> <property name='course'> <set> <value>賺錢</value> <value>情商</value> <value>心理學</value> <value>經濟學</value> <value>哲學</value> <value>英語</value> <value>數學</value> <value>計算機</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、構造器注入

<?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'><!-- 方式三:通過 類型獲取,不推薦可能會出現歧義比如兩個String--><!-- 這里以及上面為什么使用全類名? 肯定用到了反射--> <constructor-arg type='java.lang.String' value='女'/> <constructor-arg type='int' value='18'/> </bean><bean class='bing.Teacher'><!-- 我們來試一下兩個String類型會發生什么情況--> <constructor-arg type='java.lang.String' value='girl'/> <constructor-arg type='java.lang.String' value='劉老師'/></bean><!-- 相當于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本質上是什么? 就是properties,可以給簡單的屬性命名,目的是為了簡化--><bean p:name='zhangsan' p:age='18'></bean> <!-- c命名空間,怎么用? 是給constructor 構造器初始化的,這里就要求必須要有有參構造--> <bean c:name='bingbing' c:age='19' p:age='20'/></beans>

注意點:

使用前要導入:

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

類比:

p相當于標簽:properties 其實就是set注入,不過可以簡化簡單的操作

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

2.4、bean標簽作用域

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

我們可以給他設置單例模式等等

Spring入門配置和DL依賴注入實現圖解

單例模式

<bean />

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

原型模式

<bean scope='prototype'/>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产成人无精品久久久久国语 | 色综合久久久久久久 | 中文字幕日韩有码 | 欧美视频www | 国产成人综合网在线观看 | 亚洲国产一区二区三区在线观看 | 麻豆国产一区 | 91精品国产91久久久久 | 久久在现| 成人亚洲国产 | 国产精品理论 | 日本a级特黄三级三级三级 日本a一级片 | 手机国产日韩高清免费看片 | 美女综合网 | 国产女人自拍 | 成人精品视频一区二区三区 | 香蕉久久久 | 亚洲欧美视频一级 | 国产精品亚洲欧美云霸高清 | 国产一级毛片外aaaa | 国产一区二区三区四区波多野结衣 | 日本欧美一区二区三区视频 | 深夜福利视频网站 | 国产国语高清在线视频二区 | 久草视频大全 | 网禁呦萝资源网站在线观看 | 5x性区m免费毛片视频看看 | 91精品国产福利尤物免费 | 午夜91理论片 | 玖玖精品视频在线观看 | 性盈盈影院在线观看 | 毛片免费全部播放一级 | 欧美日韩免费一区二区在线观看 | 亚洲欧美一区二区三区国产精品 | 欧美aaa大片 | 国产夫妇肉麻对白 | 中文字幕毛片 | 99久久精品免费看国产四区 | 成人欧美网站免费 | 国产免费久久精品久久久 | 国产日韩一区二区三区在线播放 |