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 }

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 }

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>

执行结果

当创建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 }

执行结果:

当执行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 }

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>

执行结果

采用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 }

执行结果

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

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

时间: 2024-08-05 13:05:37

spring属性配置执行过程,单列和原型区别的相关文章

spring属性配置细节(2)

spring属性配置细节(2) Spring自动装配 XML配置里的Bean自动装配: Spring IOC容器可以自动装配Bean,需要做的是仅仅是在<bean>的autowire属性里指定自动装配的模式. 1.byName(根据名称自动装配):必须将目标Bean的名称和属性名设置的完全相同. (byName根据bean的名字和当前bean的setter风格的属性名进行自动装配 ,若有匹配的则进行自动装配,若没有匹配的,则不装配,值为null,不会出错.) <bean id="

spring属性配置细节(1)

spring属性配置细节(1) 在<constructor-arg value="240" type="int"></constructor-arg>中  ,"240"被自动转为int的240. 字面值 可用字符串表示的值,可以通过<value>元素标签或value属性进行注入. <constructor-arg type="int"> <value>240</

VS项目属性配置实验过程

一.实验背景 cocos2d-x已经发展的相对完善了,从项目的创建.编译.运行到最后的打包都有相应的便捷工具,开发者只需要关注自己的游戏逻辑代码即可,这一点很赞,可是傻瓜式的编程,让我至今还只停留在使用vs建个空项目做个小demo的阶段,我根本不知道cocos2d-x项目究竟是如何组织的,那些项目与项目之间的关系,那些库文件的引用,那些属性的配置,那些路径设置,那些宏定义究竟是在哪里,为什么会出现两个窗口,一大片属性究竟是什么意思,,所以很有必要研究一下.现在把实验过程同大家交流一下,有什么说的

Spring 属性配置

此文已由作者尧飘海授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 随着Spring的不断发展与完善,早期它的功能可能只看做是IOC(反转控制)的容器,或者其最大的亮点为DI(依赖注入),但是现在随着Spring 2.5.6 的Release发布及3.0 版的推出及其他Spring工程的发布,如(Spring Batch,web Flow),其功能越来越多. 通过上下文统一管理各种资源,通过相关设计模式完成属性的注入或实现方法,其次新的版本通过声明注入使得配置文件的大大简

spring注解配置启动过程

最近看起spring源码,突然想知道没有web.xml的配置,spring是怎么通过一个继承于AbstractAnnotationConfigDispatcherServletInitializer的类来启动自己的.鉴于能力有限以及第一次看源码和发博客,不到之处请望谅~ 我用的IDE是IntelliJ IDEA,这个比myEclipse看源码方便一点,而且黑色背景挺喜欢.然后项目是在maven下的tomcat7插件运行.spring版本是4.3.2.RELEASE. 如果写过纯注解配置的spri

Struts+Spring+Hibernate的Web应用执行过程

struts1和spring有两种整合的方法  一种是action和spring bean映射:一种是将action交给spring初始化 第一种方式:访问.do的URL->tomcat接收到request-〉到Struts配置文件里找对应的action-〉找到对应的action组件(Action那个类)-〉这个类对request进行一系列处理-〉调用spring提供的某个service的注入实例的方法->由这个方法返回值-〉响应输出 第二种方式:访问.do的URL->tomcat接收到

Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别

转: http://blog.csdn.net/it_man/article/details/5074371 Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. 总结如下: Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪

【转】 Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别

spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. 总结如下: Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSource.TransactionManager这两部分

Spring IOC的初始化过程——基于XML配置(一)

前言:在面试过程中,Spring IOC的初始化过程,基本上属于必答题,笔者的亲身经历.因此本文基于Spring的源码对其IOC的初始化过程进行总结. 注:spring版本为4.3.0. 1.调试前准备 在spring源码中,添加如下内容(有关spring源码如何导入idea,请查询相关资料): 说明: ①User为简单bean,含有name和gender两个属性. ②User.xml为spring配置文件,仅仅对User进行简单的配置. ③SpringIoCDebug为测试类. 先看执行结果: