Spring深入了解常用配置應用_第1頁
Spring深入了解常用配置應用_第2頁
Spring深入了解常用配置應用_第3頁
Spring深入了解常用配置應用_第4頁
Spring深入了解常用配置應用_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第Spring深入了解常用配置應用目錄常用配置一、別名二、bean的配置三、import存在問題總結

常用配置

現(xiàn)在這里簡單了解一下spring配置文件中的一些常用配置,在后面我們還會遇到更多的配置,在后文繼續(xù)進行介紹了。

spring中的配置一共也就這幾個

description描述不太重要,bean在之前已經(jīng)見識過了,alias給bean起別名,import在當前xml文件中導入其他xml文件

一、別名

在spring中別名主要是給bean的id起一個別名,同樣也有好幾種方式。

1、alias配置

aliasname=useralias=u/

alias是給bean的id起別名

name是bean的idalias是bean的別名

(1)先定義普通實體類

packagecom.kuang.pojo;

importlombok.Getter;

importlombok.Setter;

importlombok.ToString;

@Getter

@Setter

@ToString

publicclassUser{

privateintid;

privateStringuserName;

privateStringpassword;

}

(2)在配置文件中裝配bean,并定義bean的別名

xmlversion="1.0"encoding="UTF-8"

beansxmlns="/schema/beans"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/schema/beans

/schema/beans/spring-beans.xsd"

aliasname="user"alias="u"/

beanid="user"

propertyname="id"value="1"/

propertyname="userName"value="root"/

propertyname="password"value="123456"/

/bean

/beans

(3)通過別名也能拿到裝配的bean

publicstaticvoidmain(String[]args){

ApplicationContextcontext=newClassPathXmlApplicationContext("ApplicationContext.xml");

Useruser=context.getBean("u",User.class);

System.out.println(user);

(4)查看運行結果

二、bean的配置

也可以通過bean來配置別名,而且可以給一個bean配置多個別名

beanid="user"name="u1,u2,u3,u4"

propertyname="id"value="1"/

propertyname="userName"value="root"/

propertyname="password"value="123456"/

/bean

name就是給當前bean配置別名,可以多個別名寫在一起,中間使用空格/逗號/分號進行分割,spring都能識別

三、import

在團隊開發(fā)使用中,還是非常常見的。它可以將多個配置文件,導入合成一個

假設一個團隊中有多個人進行開發(fā),這三個人負責不同類的開發(fā),不同的類需要注冊到不同的bean中

張三beans1.xml李四beans2.xml王五beans3.xml

我們可以利用import將所有人的beans.xml合并成一個總的ApplicationContext.xml,最后使用的時候使用總的配置文件即可。

張三負責User類以及注冊到bean1.xml文件中

User類

packagecom.kuang.pojo;

importlombok.Getter;

importlombok.Setter;

importlombok.ToString;

@Getter

@Setter

@ToString

publicclassUser{

privateintid;

privateStringuserName;

privateStringpassword;

}

bean1.xml

xmlversion="1.0"encoding="UTF-8"

beansxmlns="/schema/beans"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/schema/beans

/schema/beans/spring-beans.xsd"

beanid="user"

propertyname="id"value="1"/

propertyname="userName"value="root"/

propertyname="password"value="123456"/

/bean

/beans

李四負責Student類,bean2.xml

Stduent類

packagecom.kuang.pojo;

importlombok.Getter;

importlombok.Setter;

importlombok.ToString;

@Setter

@Getter

@ToString

publicclassStudent{

privateintid;

privateStringname;

privateStringsex;

}

bean2.xml

xmlversion="1.0"encoding="UTF-8"

beansxmlns="/schema/beans"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/schema/beans

/schema/beans/spring-beans.xsd"

beanid="student"

propertyname="id"value="1"/

propertyname="name"value="張三"/

propertyname="sex"value="男"/

/bean

/beans

總的ApplicationContext.xml配置文件,導入了bean1.xml和bean2.xml

xmlversion="1.0"encoding="UTF-8"

beansxmlns="/schema/beans"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/schema/beans

/schema/beans/spring-beans.xsd"

importresource="bean1.xml"/

importresource="bean2.xml"/

/beans

使用的時候,使用總的配置文件即可

publicstaticvoidmain(String[]args){

ApplicationContextcontext=newClassPathXmlApplicationContext("ApplicationContext.xml");

Useruser=context.getBean("user",User.class);

Studentstudent=context.getBean("student",Student.class);

System.out.println(user);

System.out.println(student);

存在問題

同時使用import還存在幾個問題導入bean的id沖突

如果導入的文件中有多個重名id相同的bean

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論