Spring入門配置和DL依賴注入實(shí)現(xiàn)圖解
1、Spring入門配置
1.1、起別名
給項(xiàng)目起別名
!
1.2、import
導(dǎo)入其他xml

1.3、Bean的配置最重要的,又很多配置,我們先學(xué)一點(diǎ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 id="student" 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ì)算機(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 id="address" 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 id="user" class="bing.User">
<!-- 第一種方式:通過index獲取-->
<constructor-arg index="0" value="bingbing"/>
</bean>
<bean id="student" class="bing.Student">
<!-- 方式二:通過key value-->
<constructor-arg name="gender" value="小仙女"></constructor-arg>
<constructor-arg name="age" value="19"/>
</bean>
<bean id="student2" class="bing.Student">
<!-- 方式三:通過 類型獲取,不推薦可能會(huì)出現(xiàn)歧義比如兩個(gè)String-->
<!-- 這里以及上面為什么使用全類名? 肯定用到了反射-->
<constructor-arg type="java.lang.String" value="女"/>
<constructor-arg type="int" value="18"/>
</bean>
<bean id="teacher" class="bing.Teacher">
<!-- 我們來試一下兩個(gè)String類型會(huì)發(fā)生什么情況-->
<constructor-arg type="java.lang.String" value="girl"/>
<constructor-arg type="java.lang.String" value="劉老師"/>
</bean>
<!-- 相當(dāng)于new對(duì)象,對(duì)象名為person,只有這一個(gè)對(duì)象-->
<bean id="person" class="bing.Person">
</bean>
</beans>
注意:我們一般選用 key value注入,見名知意
2.3、拓展注入
為了簡(jiǎn)化我們可以引入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,可以給簡(jiǎn)單的屬性命名,目的是為了簡(jiǎn)化-->
<bean id="user" class="bing.pojo.User" p:name="zhangsan" p:age="18">
</bean>
<!-- c命名空間,怎么用? 是給constructor 構(gòu)造器初始化的,這里就要求必須要有有參構(gòu)造-->
<bean id="user2" class="bing.pojo.User" c:name="bingbing" c:age="19" p:age="20"/>
</beans>
注意點(diǎn):
使用前要導(dǎo)入:
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
類比:
p相當(dāng)于標(biāo)簽:properties 其實(shí)就是set注入,不過可以簡(jiǎn)化簡(jiǎn)單的操作
c詳單與 :constructor:就是用來給有參構(gòu)造器初始化的
2.4、bean標(biāo)簽作用域
bean是什么?就是一個(gè)對(duì)象,也就是類的實(shí)例
我們可以給他設(shè)置單例模式等等

單例模式
<bean id="accountService" class="com.something.DefaultAccountService"/>
<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>
原型模式
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java將List轉(zhuǎn)換為String的幾種方式
我們大家在實(shí)際開發(fā)中經(jīng)常遇到List轉(zhuǎn)為String字符串的情況,下面這篇文章主要給大家介紹了關(guān)于Java將List轉(zhuǎn)換為String的幾種方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
運(yùn)行Springboot測(cè)試類查詢數(shù)據(jù)庫數(shù)據(jù)顯示白網(wǎng)頁問題及解決方法
Spring Boot應(yīng)用未能啟動(dòng)的原因是它沒有找到合適的數(shù)據(jù)庫配置具體來說,它需要一個(gè)數(shù)據(jù)源(DataSource),但未能在你的配置中找出,也沒有找到任何嵌入式數(shù)據(jù)庫(H2, HSQL 或 Derby),本文給大家分享運(yùn)行Springboot測(cè)試類查詢數(shù)據(jù)庫數(shù)據(jù)顯示白網(wǎng)頁問題及解決方法,一起看看吧2023-11-11
springboot @Configuration和@Componment的區(qū)別及說明
這篇文章主要介紹了springboot @Configuration和@Componment的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
jstl之map,list訪問遍歷以及el表達(dá)式map取值的實(shí)現(xiàn)
下面小編就為大家?guī)硪黄猨stl之map,list訪問遍歷以及el表達(dá)式map取值的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
Spring?Boot配置文件的語法規(guī)則詳解(properties和yml)
這篇文章主要介紹了Spring?Boot配置文件的語法規(guī)則,主要介紹兩種配置文件的語法和格式,properties和yml,對(duì)于配置文件也有獨(dú)立的文件夾存放,主要用來存放一些需要經(jīng)過變動(dòng)的數(shù)據(jù)(變量值),感興趣的朋友跟隨小編一起看看吧2024-07-07
Java應(yīng)用程序CPU100%問題排查優(yōu)化實(shí)戰(zhàn)
這篇文章主要介紹了如何排查和優(yōu)化Java應(yīng)用程序CPU使用率達(dá)到100%的問題,文中通過代碼示例和圖文結(jié)合的方式講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2025-02-02

