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 筛选合适的通知器,以及如何创建代理对象的过程.现在我们的得 ...
随机推荐
- 自学PHP的野方法
1.基础知识 最早的基础知识仅限于那么一点点的html和css,比牛毛还牛毛的一点点.所以最开始是从immoc上看视频和跟着练习,花了有一个多月,看完一个路径从:零开始学习ThinkPHP框架,由于基 ...
- 一些使用的linux库
1.curl网络库 apt-get install libcurl4-openssl-dev sudo apt-get install curl 2.jsoncpp库 sudo apt-get ins ...
- JAVA array,map 转 json 字符串
public class User { private String username; private String password; public String getUsername() { ...
- 测试嵌入GeoGebra网页
使用 http://ggbstudy.top/tools/ggb2html/ 将GGB文件免费托管,然后在博客内容中点击“HTML”按钮插入GGB网页地址: <iframe src=" ...
- [Delphi]编译条件
当软件在多个DELPHI版本下编译时,需要处理各版本的不同情况,使用编译条件技术实现. 万一博客,编译指令基础使用介绍:http://www.cnblogs.com/del/category/1686 ...
- [leetcode] 13. Remove Duplicates from Sorted List
这个题目其实不难的,主要是我C++的水平太差了,链表那里绊了好久,但是又不像用python,所以还是强行上了. 题目如下: Given a sorted linked list, delete all ...
- django设置cookie和session
1 设置cookie 本例中应用名称为cookie 模型model from django.db import models from django.db import models class Us ...
- Postgresql 日志收集
PG安装完成后默认不会记录日志,必须修改对应的(${PGDATA}/postgresql.conf)配置才可以,这里只介绍常用的日志配置. 1.logging_collector = on/off - ...
- 异步和等待(async和await)
在.Net 4.5中,通过async和await两个关键字,引入了一种新的基于任务的异步编程模型(TAP).在这种方式下,可以通过类似同步方式编写异步代码,极大简化了异步编程模型.如下式一个简单的实例 ...
- UWP开发入门(三)——{x:Bind}扩展标记
上周打炉石打得太晚……忘记更新了,本周补上.本篇我们讲一下{x:Bind}扩展标记.{x:Bind}扩展标记也是Windows 10 Uinversal 新增的内容,按官方的说法是 {Binding} ...