易学笔记--第2章:spring中的Bean/2.3 Bean的作用域

第2章:spring中的Bean/2.3 Bean的作用域/2.3.1 单例:singleton

  1. 概念:指的是所创建的Bean在整个声明周期中都是唯一的一个对象,也就是所谓的单例模式
  2. 在声明bean的时候如果省略则默认为默认为singleton作用域

第2章:spring中的Bean/2.3 Bean的作用域/2.3.2  原型作用域:prototype

  1. 指的是每次getBean操作或者引用操作(ref bean)都会创建一个新的Bean,类似于每次都会new一个新的对象

第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配置方式

  1. 单例:singleton: 第2章:spring中的Bean/2.3 Bean的作用域/2.3.1 单例:singleton
    1. 类的构造方法:

      AccountDaoInMemoryImpl(){

                 System.out.println("AccountDaoInMemoryImpl类初始化");

           }

    2. XML定义:

      <!-- 被依赖的bean的定义 -->

           <bean id="accountDao" class="com.wiley.beginningspring.ch2.AccountDaoInMemoryImpl" scope="singleton">

           </bean>

    3. 调用语句:

      AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

      AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);

            AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);

    4. 调用结果:只被初始化一次

      AccountDaoInMemoryImpl类初始化

  2. 原型作用域:prototype:第2章:spring中的Bean/2.3 Bean的作用域/2.3.2 原型作用域:prototype
    1. 类的构造方法:

      AccountDaoInMemoryImpl(){

                 System.out.println("AccountDaoInMemoryImpl类初始化");

           }

    2. XML定义:

      <!-- 被依赖的bean的定义 -->

           <bean id="accountDao" class="com.wiley.beginningspring.ch2.AccountDaoInMemoryImpl" scope="prototype">

           </bean>

    3. 调用语句:调用语句:

      AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

      AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);

            AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);

    4. 调用结果:会被初始化多次

      AccountDaoInMemoryImpl类初始化

      AccountDaoInMemoryImpl类初始化

      AccountDaoInMemoryImpl类初始化

  3.  

第2章:spring中的Bean/2.3 Bean的作用域/2.3.6  作用域举例:JAVA配置方式

  1. 单例:singleton: 第2章:spring中的Bean/2.3 Bean的作用域/2.3.1 单例:singleton
    1. 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;

           }

    2. 调用语句:

      AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

      AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);

            AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);

    3. 调用结果:只被初始化一次

      AccountDao方法被调用

  2. 原型作用域:prototype:第2章:spring中的Bean/2.3 Bean的作用域/2.3.2 原型作用域:prototype
    1. 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;

           }

    2. 调用语句:

      AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

      AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);

            AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);

    3. 调用结果:会被初始化多次

      AccountDao方法被调用

      AccountDao方法被调用

      AccountDao方法被调用


第2章:spring中的Bean/2.3 Bean的作用域/2.3.7  作用域举例:注解方式

  1. 单例:singleton: 第2章:spring中的Bean/2.3 Bean的作用域/2.3.1 单例:singleton
    1. 类的构造方法:

      AccountDaoInMemoryImpl(){

                 System.out.println("AccountDaoInMemoryImpl类初始化");

           }

    2. 注解:

      @Repository("accountDao")

      @Scope("singleton ")

      public class AccountDaoInMemoryImpl implements AccountDao {

    3. 调用语句:

      AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

      AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);

            AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);

    4. 调用结果:只被初始化一次

      AccountDaoInMemoryImpl类初始化

  1. 原型作用域:prototype:第2章:spring中的Bean/2.3 Bean的作用域/2.3.2 原型作用域:prototype
    1. 类的构造方法:

      AccountDaoInMemoryImpl(){

                 System.out.println("AccountDaoInMemoryImpl类初始化");

           }

    2. 注解:

      @Repository("accountDao")

      @Scope("prototype ")

      public class AccountDaoInMemoryImpl implements AccountDao {

    3. 调用语句:调用语句:

      AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

      AccountDao accountDao1 = applicationContext.getBean("accountDao", AccountDao.class);

            AccountDao accountDao2 = applicationContext.getBean("accountDao", AccountDao.class);

    4. 调用结果:会被初始化多次

      AccountDaoInMemoryImpl类初始化

      AccountDaoInMemoryImpl类初始化

      AccountDaoInMemoryImpl类初始化

猜你喜欢

转载自blog.csdn.net/u011830122/article/details/83901836