spring属性配置执行过程,单列和原型区别
Spring配置中,采用属性注入时,当创建IOC容器时,也直接创建对象,并且执行相对应的setter方法
Student.java
package com.scope;
public class Student {
private String name;
private String number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println(name);
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Student() {
super();
System.out.println("hello Student!");
}
}
Main.java
package com.scope; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml"); }
}
beans-scope.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student1" class="com.scope.Student">
<property name="name" value="Mary"></property>
<property name="number" value="1120143231"></property>
</bean>
<bean id="student2" class="com.scope.Student">
<property name="name" value="Curry"></property>
<property name="number" value="1120111413"></property>
</bean> </beans>
执行结果

当创建IOC容器时,配置文件中有多少个bean就会创建多少个对象,并且执行相对应的setter函数。
我们对Main.java进行修改
package com.scope; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
Student student1 = (Student) ctx.getBean("student1");
Student student2 = (Student) ctx.getBean("student1");
System.out.println(student1 == student2);
}
}
执行结果:

当执行Student student1 = (Student) ctx.getBean("student1")时,并没有创建对象,只是创建了一个索引而已,Student1和Student2引用的是同一个对象。
以上就是单例模式,属性注入时,默认的就是单例模式,每个bean id只会创建一个对象,并且在创建IOC容器时,就创建对象和执行相对应的setter函数。
下面讲原型模式,即prototype模式。
Main.java
package com.scope; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
// Student student1 = (Student) ctx.getBean("student1");
// Student student2 = (Student) ctx.getBean("student1");
// System.out.println(student1 == student2);
}
}
beans-scope.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student1" class="com.scope.Student" scope="prototype">
<property name="name" value="Mary"></property>
<property name="number" value="1120143231"></property>
</bean>
<bean id="student2" class="com.scope.Student" scope="prototype">
<property name="name" value="Curry"></property>
<property name="number" value="1120111413"></property>
</bean> </beans>
执行结果

采用prototype模式时,在创建IOC容器时,并没有创建相应的对象。我们继续对Main.java进行修改。
package com.scope; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
Student student1 = (Student) ctx.getBean("student1");
Student student2 = (Student) ctx.getBean("student1");
System.out.println(student1 == student2);
}
}
执行结果

在执行Student student1 = (Student) ctx.getBean("student1"); Student student2 = (Student) ctx.getBean("student1");创建了两个对象,所以输出了false。
采用prototype模式时,只有在获取bean时,才开始创建对象,获取多少次就创建多少个对象。
spring属性配置执行过程,单列和原型区别的更多相关文章
- VS项目属性配置实验过程
(原创,转载注明出处:http://www.cnblogs.com/binxindoudou/p/4017975.html ) 一.实验背景 cocos2d-x已经发展的相对完善了,从项目的创建.编译 ...
- Spring 属性配置
此文已由作者尧飘海授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 随着Spring的不断发展与完善,早期它的功能可能只看做是IOC(反转控制)的容器,或者其最大的亮点为DI( ...
- spring注解配置启动过程
最近看起spring源码,突然想知道没有web.xml的配置,spring是怎么通过一个继承于AbstractAnnotationConfigDispatcherServletInitializer的 ...
- spring 属性配置细节
1.使用构造器注入属性值可以指定参数的位置和参数的类型!以区分重载的构造器.例如:<constructor-arg value="" type="java.lang ...
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Struts+Spring+Hibernate的Web应用执行过程
struts1和spring有两种整合的方法 一种是action和spring bean映射:一种是将action交给spring初始化 第一种方式:访问.do的URL->tomcat接收到r ...
- Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别
转: http://blog.csdn.net/it_man/article/details/5074371 Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之 ...
- spring MVC处理请求过程及配置详解
本文主要梳理下Spring MVC处理http请求的过程,以及配置servlet及业务application需要的常用标签,及其包含的意义. spring MVC处理请求过程 首先看一个整体图 简单说 ...
- Spring AOP 源码分析 - 拦截器链的执行过程
1.简介 本篇文章是 AOP 源码分析系列文章的最后一篇文章,在前面的两篇文章中,我分别介绍了 Spring AOP 是如何为目标 bean 筛选合适的通知器,以及如何创建代理对象的过程.现在我们的得 ...
随机推荐
- java常量池与对象存储
一 数据存储位置 我们先来谈谈数据的存储位置,有五个地方可以存储数据 (1)寄存器:这是最快的存储区,因为它位于不同于其他存储区的地方- ...
- sql查询磁盘空间并发预警邮件
检测磁盘空间,如果低于设置的预警值则发出一封预警邮件,这样的事情可以用SQL server的作业可以做,关键SQL语句如下例子所示: DECLARE @TableText NVARCHAR(MAX)= ...
- genymotion的安装
1.安装virtualBox google的模拟器是运行在qemu上面的. genymotion这个模拟器运行在virtualBox上面.
- UVa 10570 Meeting with Aliens (暴力)
题意:给定一个排列,每次可交换两个数,用最少的次数把它变成一个1~n的环状排列. 析:暴力题.很容易想到,把所有的情况都算一下,然后再选出次数最少的那一个,也就是说,我们把所有的可能的形成环状排列全算 ...
- 聚合函数 listagg (超出长度限制时xmlagg)
表&数据 ),buy ),price NUMBER); ); ); ); 原来的结果 SELECT * FROM PEOPLEBUY ORDER BY PEOPLE; 想要的结果 SELECT ...
- dojoConfig包的配置(7/26号夜)
主页代码: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...
- Restful风格wcf调用3——Stream
写在前面 上篇文章介绍了restful接口的增删改查,本篇文章将介绍,如何通过数据流进行文件的上传及下载操作. 系列文章 Restful风格wcf调用 Restful风格wcf调用2——增删改查 一个 ...
- java并发编程工具类辅助类:CountDownLatch、CyclicBarrier和 Semaphore
在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法. 以下 ...
- visual studio code中使用emmet插件在.vue文件失效
使用visual studio code编辑.vue文件时,emmet插件无法使用,可以通过以下两种试解决: 1.文件→设置,在右侧窗口添加以下代码: "emmet.syntaxProfil ...
- .NET框架源码解读之准备CLR源码阅读环境
微软发布了CLR 2.0的源码,这个源码是可以直接在freebsd和windows环境下编译及运行的,请在微软shared source cli(http://www.microsoft.com/en ...