spring 註解 配置元件

2021-10-24 05:24:58 字數 3746 閱讀 4306

@configuration + @bean

@scope

@lazy

@conditional

@import

載入指定包,預設載入@component、@controller、@service、@repository

限定載入範圍

@componentscan

(value =

"com.example.demo"

,includefilters = @componentscan.

filter

( type = filtertype.annotation,

classes =),

// 只載入@service

usedefaultfilters = false)

// usedefaultfilters 必須設定成false,否則filter不生效,預設載入所有。

includefilters.type

type = filtertype.annotation,classes =

//按註解(@service)過濾

type = filtertype.assignable_type,classes =

//按指定類(***.class)過濾

type = filtertype.custom, classes =

//按自定義的filter過濾

public class mytypefilter implements typefilter 

return false;

}}

@configuration

@componentscan

(value =

"com.example.demo.configuration"

)public class myconfig

@bean

(name=

"person1"

)//自定義物件名 : person1

}

@scope

("prototype"

)// 原型模式

@configuration

public class myconfig

}

@scope() 預設單例

@scope(「singelton」) 單例模式,即每次呼叫返回同乙個例項

@scope(「prototype」) 原型模式,即每次呼叫返回乙個新的例項

@scope(「request」) 每次請求建立乙個例項(主要應用於web

@scope(「session」) 同一session只建立乙個例項(主要應用於web)

public class mytest 

}

延遲載入,預設true,容器啟動時,不建立物件,物件呼叫時才建立。

懶載入只針對單例bean起作用

不新增@lazy,啟動時載入bean

@configuration

public class myconfig

}public class mytest

}

輸出結果:

將物件新增到ioc容器中

ioc容器建立完成

新增 @lazy

@configuration

public class myconfig

}

輸出結果:

ioc容器建立完成

將物件新增到ioc容器中

滿足條件則註冊、裝載,spring4開始提供。

根據不同的系統,例項化不同的物件。

可通過修改vm引數:-ea dos.name=linux,來嘗試linux系統的情況。

@configuration

public class myconfig

@conditional

(linuxcondition.class)

@bean

public person lily()

}

public class wincondition implements condition 

return false;

}}

public class linuxcondition implements condition 

return false;

}}

並不是所有的外掛程式都遵循spring.factories規則。比如 redisautoconfiguration類,基於@conditionalonclass()條件控制,裝載redis的配置。

spring-autoconfigure-metadata.properties 檔案

resources\meta-inf目錄下建立該檔案,表示如果引用的上層專案中存在com.gupaoedu.democlass該類,則載入當前專案中類。

檔案內容:

com.gupaoedu.autoconfiguration.demo.gupaoconfiguration.conditionalonclass=com.gupaoedu.democlass

直接匯入:

@configuration

@import

(value =

)//直接匯入 cat.class

public class myconfig

自定義規則匯入:實現 importselector 介面

@configuration

@import

(value =

)public class myconfig

public class myimportselector implements importselector

;// 傳入需要匯入的配置類、或者bean

}}

手動注入:實現importbeandefinitionregistrar 介面

@import

(value =

)public class myconfig

public class myimportbeandefinitionregistrar implements importbeandefinitionregistrar 

}}

通過factorybean注入:實現 factorybean 介面

@configuration

public class myconfig

}

public class myfactorybean implements factorybean

@nullable

@override

public class<

?>

getobjecttype()

@override

public boolean issingleton()

}

通過 issingleton() 控制是否單例

測試類:

public class mytest 

}

spring註解和元件掃瞄配置

spring的部分bean在xml中配置,但我覺得action,service,dao這些完全沒必要在xml中配置,一來如果ide支援不好容易配置錯誤,二來這些類多起來後簡直就沒辦法看。故採用註解配置這三層,基本配置配好後可以不動xml配置檔案了。一 首先新增註解和元件掃瞄配置。在spring配置檔...

spring元件註解

1 controller 控制器 注入服務 2 service 服務 注入dao 3 repository dao 實現dao訪問 4 component 把普通pojo例項化到spring容器中,相當於配置檔案中的 component,service,controller,repository註解...

spring 註解配置

以前我們在配置spring檔案的時候一般都是這麼寫 autowire有4種自動裝配的型別 byname 把與bean的屬性具有相同名字 或者id 的其他bean自動配置到bean對應的屬性中。bytype 把與bean的屬性具有相同型別的其他bean自動配置到bean對應的屬性中。construct...