易學筆記--第2章:spring中的Bean/2.3 Bean的作用域
第2章:spring中的Bean/2.3 Bean的作用域/2.3.1 單例:singleton
第2章:spring中的Bean/2.3 Bean的作用域/2.3.2 原型作用域:prototype
第2章:spring中的Bean/2.3 Bean的作用域/2.3.3 請求作用域:request
第2章:spring中的Bean/2.3 Bean的作用域/2.3.4 會話作用域:session
第2章:spring中的Bean/2.3 Bean的作用域/2.3.5 作用域舉例:XML配置方式
- 單例:singleton: 第2章:spring中的Bean/2.3 Bean的作用域/2.3.1 單例:singleton
- 類的構造方法:
AccountDaoInMemoryImpl(){
System.out.println("AccountDaoInMemoryImpl類初始化");
}
- XML定義:
<!-- 被依賴的bean的定義 -->
<bean id="accountDao" class="com.wiley.beginningspring.ch2.AccountDaoInMemoryImpl" scope="singleton">
</bean>
- 呼叫語句:
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);
AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);
- 呼叫結果:只被初始化一次
AccountDaoInMemoryImpl類初始化
- 類的構造方法:
- 原型作用域:prototype:第2章:spring中的Bean/2.3 Bean的作用域/2.3.2 原型作用域:prototype
- 類的構造方法:
AccountDaoInMemoryImpl(){
System.out.println("AccountDaoInMemoryImpl類初始化");
}
- XML定義:
<!-- 被依賴的bean的定義 -->
<bean id="accountDao" class="com.wiley.beginningspring.ch2.AccountDaoInMemoryImpl" scope="prototype">
</bean>
- 呼叫語句:呼叫語句:
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);
AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);
- 呼叫結果:會被初始化多次
AccountDaoInMemoryImpl類初始化
AccountDaoInMemoryImpl類初始化
AccountDaoInMemoryImpl類初始化
- 類的構造方法:
第2章:spring中的Bean/2.3 Bean的作用域/2.3.6 作用域舉例:JAVA配置方式
- 單例:singleton: 第2章:spring中的Bean/2.3 Bean的作用域/2.3.1 單例:singleton
- Bean方法:
@Bean
@Scope("singleton")
public AccountDao accountDao() {
System.out.println("AccountDao方法被呼叫");
AccountDao bean = new AccountDaoInMemoryImpl();
//depedencies of accountDao bean will be injected here...
return bean;
}
- 呼叫語句:
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);
AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);
- 呼叫結果:只被初始化一次
AccountDao方法被呼叫
- Bean方法:
- 原型作用域:prototype:第2章:spring中的Bean/2.3 Bean的作用域/2.3.2 原型作用域:prototype
- Bean方法 :
@Bean
@Scope("prototype")
public AccountDao accountDao() {
System.out.println("AccountDao方法被呼叫");
AccountDao bean = new AccountDaoInMemoryImpl();
//depedencies of accountDao bean will be injected here...
return bean;
}
- 呼叫語句:
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);
AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);
- 呼叫結果:會被初始化多次
AccountDao方法被呼叫
AccountDao方法被呼叫
AccountDao方法被呼叫
- Bean方法 :
第2章:spring中的Bean/2.3 Bean的作用域/2.3.7 作用域舉例:註解方式
-
- 類的構造方法:
AccountDaoInMemoryImpl(){
System.out.println("AccountDaoInMemoryImpl類初始化");
}
- 註解:
@Repository("accountDao")
@Scope("singleton ")
public class AccountDaoInMemoryImpl implements AccountDao {
- 呼叫語句:
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);
AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);
- 呼叫結果:只被初始化一次
AccountDaoInMemoryImpl類初始化
- 類的構造方法:
- 原型作用域:prototype:第2章:spring中的Bean/2.3 Bean的作用域/2.3.2 原型作用域:prototype
-
- 類的構造方法:
AccountDaoInMemoryImpl(){
System.out.println("AccountDaoInMemoryImpl類初始化");
}
- 註解:
@Repository("accountDao")
@Scope("prototype ")
public class AccountDaoInMemoryImpl implements AccountDao {
- 呼叫語句:呼叫語句:
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);
AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);
- 呼叫結果:會被初始化多次
AccountDaoInMemoryImpl類初始化
AccountDaoInMemoryImpl類初始化
AccountDaoInMemoryImpl類初始化
- 類的構造方法: