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 筛选合适的通知器,以及如何创建代理对象的过程.现在我们的得 ...
随机推荐
- Multithreading in C
Multithreading in C, POSIX(可移植操作系统接口Portable Operating System Interface X ) style Multithreading - A ...
- QuartJob的CronExpressionString规则详解
字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? ...
- 删除重复记录的最新sql脚本
delete from tb1 where id in(select t2.minnum from(select MIN(t1.id) as minnum,t1.col1 as ars ,COUN ...
- IntelliJ IDEA 2017版 快捷键CTRL + SHIFT + A无效如何调试(详细的开启idea自动make功能 )
1.前景描述 因为我把编译器的快捷键都设置成eclipse模式了,所以要做热部署的时候,需要CTRL + SHIFT + A --> 查找Registry --> 找到并勾选compile ...
- 我的border能自定义四角之border-radius : 左上角,右上角,左下角,右下角。
1 边框:border: 1px solid #0081df; 2 想要单独加上四个圆角: border-bottom-left-radius: 5px; border-top-left-radius ...
- Hadoop中Writable类之四
1.定制Writable类型 Hadoop中有一套Writable实现,例如:IntWritable.Text等,但是,有时候可能并不能满足自己的需求,这个时候,就需要自己定制Writable类型. ...
- Python作图笔记
感谢莫烦大神,附带他的个人网站链接:https://morvanzhou.github.io/ 再带上官方的文档,多看文档啊!不然参数忘了就没地方查了:https://matplotlib.org/a ...
- Delphi 文件拷贝
function DoCopyDir(sDirName:String;sToDirName:String):Boolean;var hFindFile:Cardinal; t,tfile:String ...
- WEBXONE IIS部署C/S程序
WEBXONE IIS部署C/S程序 在EXE的主窗体的ONCREATE()里添加如下代码,部署的时候记得带wxoBase.dll. uses wxoExec; procedure TFrmMain. ...
- Solr 从文件创建索引
http://blog.csdn.net/clj198606061111/article/details/21492457 http://wiki.apache.org/solr/Extracting ...