• spring属性配置执行过程,单列和原型区别


      Spring配置中,采用属性注入时,当创建IOC容器时,也直接创建对象,并且执行相对应的setter方法

    Student.java

     1 package com.scope;
     2 
     3 public class Student {
     4     private String name;
     5     private String number;
     6     public String getName() {
     7         return name;
     8     }
     9     public void setName(String name) {
    10         this.name = name;
    11         System.out.println(name);
    12     }
    13     public String getNumber() {
    14         return number;
    15     }
    16     public void setNumber(String number) {
    17         this.number = number;
    18     }
    19     public Student() {
    20         super();
    21         System.out.println("hello Student!");
    22         
    23     }
    24     
    25     
    26 }
    View Code

    Main.java

     1 package com.scope;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 public class Main {
     7     public static void main(String[] args) {
     8         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
     9         
    10     }
    11 }
    View Code

    beans-scope.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     5 
     6     <bean id="student1" class="com.scope.Student">
     7         <property name="name" value="Mary"></property>
     8         <property name="number" value="1120143231"></property>
     9     </bean>
    10     <bean id="student2" class="com.scope.Student">
    11         <property name="name" value="Curry"></property>
    12         <property name="number" value="1120111413"></property>
    13     </bean>
    14     
    15 </beans>
    View Code

    执行结果

    当创建IOC容器时,配置文件中有多少个bean就会创建多少个对象,并且执行相对应的setter函数。

    我们对Main.java进行修改

     1 package com.scope;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 public class Main {
     7     public static void main(String[] args) {
     8         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
     9         Student student1 = (Student) ctx.getBean("student1");
    10         Student student2 = (Student) ctx.getBean("student1");
    11         System.out.println(student1 == student2);
    12     }
    13 }
    View Code

    执行结果:

    当执行Student student1 = (Student) ctx.getBean("student1")时,并没有创建对象,只是创建了一个索引而已,Student1和Student2引用的是同一个对象。

    以上就是单例模式,属性注入时,默认的就是单例模式,每个bean id只会创建一个对象,并且在创建IOC容器时,就创建对象和执行相对应的setter函数。

    下面讲原型模式,即prototype模式。

    Main.java

     1 package com.scope;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 public class Main {
     7     public static void main(String[] args) {
     8         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
     9 //        Student student1 = (Student) ctx.getBean("student1");
    10 //        Student student2 = (Student) ctx.getBean("student1");
    11 //        System.out.println(student1 == student2);
    12     }
    13 }
    View Code

    beans-scope.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     5 
     6     <bean id="student1" class="com.scope.Student" scope="prototype">
     7         <property name="name" value="Mary"></property>
     8         <property name="number" value="1120143231"></property>
     9     </bean>
    10     <bean id="student2" class="com.scope.Student" scope="prototype">
    11         <property name="name" value="Curry"></property>
    12         <property name="number" value="1120111413"></property>
    13     </bean>
    14     
    15 </beans>
    View Code

    执行结果

    采用prototype模式时,在创建IOC容器时,并没有创建相应的对象。我们继续对Main.java进行修改。

     1 package com.scope;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 public class Main {
     7     public static void main(String[] args) {
     8         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
     9         Student student1 = (Student) ctx.getBean("student1");
    10         Student student2 = (Student) ctx.getBean("student1");
    11         System.out.println(student1 == student2);
    12     }
    13 }
    View Code

    执行结果

    在执行Student student1 = (Student) ctx.getBean("student1"); Student student2 = (Student) ctx.getBean("student1");创建了两个对象,所以输出了false。

    采用prototype模式时,只有在获取bean时,才开始创建对象,获取多少次就创建多少个对象。
     

  • 相关阅读:
    团队-团队编程项目作业名称-最终程序
    《团队-中国象棋-项目总结》
    课后作业-阅读任务-阅读提问-4
    《20171130-构建之法:现代软件工程-阅读笔记4》
    《软件工程课程总结》
    《20171117-构建之法:现代软件工程-阅读笔记-3》
    -课后作业-阅读任务-阅读提问-3
    《团队-中国象棋-团队一阶段互评》
    《团队-中国象棋-开发文档》
    《结对-贪吃蛇-结对项目总结》
  • 原文地址:https://www.cnblogs.com/Hxinguan/p/5936901.html
Copyright © 2020-2023  润新知