給容器中註冊元件的方式 筆記

2021-09-01 13:34:26 字數 475 閱讀 3985

1.包掃瞄+元件標註註解(@controller/@service/@repository/@component) [這種方式侷限自己寫的類]

2.@bean[匯入的第三方包裡面的元件]

3.@import[可以快速給容器中匯入乙個或者多個元件]

3.1、@import(要匯入到容器中的元件):容器中就會自動註冊這個元件,id預設是全類名

3.2、importselector:返回需要匯入的元件的全類名陣列  (在springboot用的非常多)

3.3、importbeandefinitionregistrar:手動註冊bean到容器中

4.使用spring提供的factorybean(工廠bean):在spring與其他框架整合時,用的比較多

4.1、預設獲取到的是工廠bean呼叫getobject建立的物件

4.2、要獲取工廠bean本身,需要在給id前面加乙個& (即:&colo***ctorybean)

Spring註解 給容器註冊元件的幾種方式

1.2 方式二 1.1.1 repository service controller component 1.1.2 說明 repository service controller component這四個註解都是標註在元件 類 上,用來把元件註冊到spring容器中。註冊的每個元件,型別是它本...

spring容器註冊元件的方式

給容器註冊元件的方式 1.包掃瞄 元件標註註解 controller servuce repostiory component 侷限自己寫的類 1 配置檔案的方式 在com.dingaimin包下的並使用 controller servuce repostiory component 註解的類會被載...

給spring容器中註冊bean元件的幾種方法

在xml檔案中新增包掃瞄,然後在需要註冊的元件上標註註解 controller service repository component 使用 bean 註解匯入第三方包裡的元件 bean win public student student03 註解 import,快速給容器中匯入乙個元件 看一下...