zoukankan      html  css  js  c++  java
  • spring in action 学习笔记九:如何证明在scope为prototype时每次创建的对象不同。

      spring 中scope的值有四个:分别是:singleton、prototype、session、request。其中session和request是在web应用中的。

    下面证明当scope为prototype时每次创建的对象是不同的。

    示例代码如下:

     1 package com.advancedWiring.ambiguityIniAutowiring2;
     2 
     3 import org.springframework.beans.factory.BeanNameAware;
     4 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
     5 import org.springframework.context.annotation.Scope;
     6 import org.springframework.stereotype.Component;
     7 
     8 /**
     9  * Created by ${秦林森} on 2017/6/9.
    10  */
    11 @Component
    12 @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    13 public class Student implements BeanNameAware{
    14     private  String name;
    15     private Integer age;
    16 
    17     public String getName() {
    18         return name;
    19     }
    20 
    21     public void setName(String name) {
    22         this.name = name;
    23     }
    24 
    25     public Integer getAge() {
    26         return age;
    27     }
    28 
    29     public void setAge(Integer age) {
    30         this.age = age;
    31     }
    32 
    33     @Override
    34     public void setBeanName(String name) {
    35         System.out.println("the Student bean name is :"+name);
    36     }
    37 }
     1 package com.advancedWiring.ambiguityIniAutowiring2;
     2 
     3 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
     4 import org.springframework.context.annotation.Bean;
     5 import org.springframework.context.annotation.ComponentScan;
     6 import org.springframework.context.annotation.Configuration;
     7 import org.springframework.context.annotation.Scope;
     8 
     9 /**
    10  * Created by ${秦林森} on 2017/6/9.
    11  */
    12 @Configuration
    13 @ComponentScan
    14 public class StudentConfig {
    15 
    16 }
     1 package com.advancedWiring.ambiguityIniAutowiring2;
     2 
     3 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 /**
     7  * Created by ${秦林森} on 2017/6/9.
     8  */
     9 public class Test {
    10     public static void main(String[] args) {
    11     
    12         AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(StudentConfig.class);
    13         Student student = ac.getBean(Student.class);
    14         Student student2 = ac.getBean(Student.class);
    15         System.out.println(student==student2);
    16     }
    17 }/**output:
    false/

    上面的测试类的运行结果是false,就说明了在scope为prototype时,spring每次创建的bean都是一个全新的对象。

  • 相关阅读:
    用三张图宏观把握数据库
    MySQL数据库安装与配置详解(图文)
    如何彻底的删除MySQL数据库(图文教程)
    几个常用网络/服务器监控开源软件
    tomcat服务器虚拟目录的映射方式
    手动开发动态资源之servlet初步
    软件项目开发没规划好就注定会失败
    在系统设计中,如何控制层次的问题
    软件系统架构中的分层思想
    鸟哥谈PHP的架构与未来发展
  • 原文地址:https://www.cnblogs.com/1540340840qls/p/6971485.html
Copyright © 2011-2022 走看看