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

摘要:
在Spring配置中,当使用属性注入时,当创建IOC容器时,也会直接创建对象,并使用相应的setter方法Student.java1packagecom。执行范围;23publicclassStudent{4privateStringname;5privateStringnumber;6publicStringgetName(){7returnname;8}9publicvoidsetName

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

Student.java

spring属性配置执行过程,单列和原型区别第1张spring属性配置执行过程,单列和原型区别第2张
 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

spring属性配置执行过程,单列和原型区别第3张spring属性配置执行过程,单列和原型区别第4张
 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

spring属性配置执行过程,单列和原型区别第5张spring属性配置执行过程,单列和原型区别第6张
 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

执行结果

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

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

我们对Main.java进行修改

spring属性配置执行过程,单列和原型区别第8张spring属性配置执行过程,单列和原型区别第9张
 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

执行结果:

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

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

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

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

Main.java

spring属性配置执行过程,单列和原型区别第11张spring属性配置执行过程,单列和原型区别第12张
 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

spring属性配置执行过程,单列和原型区别第13张spring属性配置执行过程,单列和原型区别第14张
 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

执行结果

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

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

spring属性配置执行过程,单列和原型区别第16张spring属性配置执行过程,单列和原型区别第17张
 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

执行结果

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

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

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

免责声明:文章转载自《spring属性配置执行过程,单列和原型区别》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇[转]history.back(-1)和history.go(-1)的区别SQL Server 2005安装详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

邮政编码的正则表达式

有关代码: package TestRegex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test01 { /** * 邮政编码正则:"^[1-9]\d{5}$" * ^:正则开始符 * $:正则结束符...

shell中的(),{}几种语法用法

转自:https://www.cnblogs.com/HKUI/p/6423918.html 查看脚本语法是否有错误:bash -n modify_suffix.sh跟踪执行sh -x modify_suffix.sh aaa 1.${var} 2.$(cmd) 3.()和{} 4.${var:-string},${var:+string},...

Solon详解(一)- 快速入门

Solon详解系列文章:Solon详解(一)- 快速入门Solon详解(二)- Solon的核心Solon详解(三)- Solon的web开发Solon详解(四)- Solon的事务传播机制Solon详解(五)- Solon扩展机制之Solon PluginSolon详解(六)- 定制业务级别的验证注解 一、Solon 最近号称小而美的的Solon框架,终...

java 注解 Annontation

什么是注解?   对于很多初次接触的开发者来说应该都有这个疑问?Annontation是Java5开始引入的新特征,中文名称叫注解。它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metadata)与程序元素(类、方法、成员变量等)进行关联。为程序的元素(类、方法、成员变量)加上更直观更明了的说明,这些说明信息是与程序的业务逻辑无关,并且供指定...

C++20新特性

C++20新特性 新增关键字(keywords) concept requires constinit consteval co_await co_return co_yield char8_t 模块(Modules) 优点: 1)没有头文件; 2)声明实现仍然可分离, 但非必要; 3)可以显式指定导出哪些类或函数; 4)不需要头文件重复引入宏 (incl...

Swift-可选值(Optional)讲解

前提:Swift中有规定:对象中的任何属性在创建时,都必须要有明确的初始化值 1.定义可选类型 方式一:常规方式(不常用) var name : Optional<String> = nil 方式二:语法糖(常用) var name:String? = nil Optional理解:   Optional也是Objective-C没有的数据类型...