易學筆記--第2章:spring中的Bean/2.4 延遲初始化
阿新 • • 發佈:2018-11-11
第2章:spring中的Bean/2.4 延遲初始化/2.4.1 概念
第2章:spring中的Bean/2.4 延遲初始化/2.4.2 延遲操作實現/2.4.2.1 XML方式
- 設定全部的Bean都延遲
- 設定某個Bean延遲
第2章:spring中的Bean/2.4 延遲初始化/2.4.2 延遲操作實現/2.4.2.2 JAVA配置方式
- 使用@Lazy註解,其中true表示延遲,false表示不延遲
- 舉例:
- 配置類定義:
public AccountService accountService() {
System.out.println("accountService方法被呼叫");
AccountServiceImpl bean = new AccountServiceImpl();
bean.setAccountDao(accountDao());
public AccountDao accountDao() {
System.out.println("AccountDao方法被呼叫");
AccountDao bean = new AccountDaoInMemoryImpl();
- 呼叫結果:
- 配置類定義:
第2章:spring中的Bean/2.4 延遲初始化/2.4.2 延遲操作實現/2.4.2.3 註解方式
- 在註解類上面也是使用@Lazy註解進行標識
- 舉例:
- 註解類:
- 呼叫結果:
第2章:spring中的Bean/2.4 延遲初始化/2.4.3 被依賴的Bean:延遲不生效
- 如果某個Bean(比如A)被其它的Bean依賴,如果其它Bean在spring容器啟動時被初始化,那麼某個Bean(比如A)的延遲機制不會生效
- 舉例: