第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类初始化
- 类的构造方法: