Spring入门第八课
看如下代码
<?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="car" class="logan.spring.study.autowire.Car">
<property name="brand" value="Audi"></property>
<property name="price" value="3000000"></property>
</bean>
</beans>
package logan.spring.study.scope; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import logan.spring.study.autowire.Car; public class Main { public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml"); Car car1 = (Car) ctx.getBean("car");
Car car2 = (Car) ctx.getBean("car"); System.out.println(car1 == car2); } }
输出结果为:
五月 20, 2017 4:39:17 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Sat May 20 16:39:17 CST 2017]; root of context hierarchy
五月 20, 2017 4:39:17 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-scope.xml]
true
可以看到从IOT获得的Car实例是单例的。
事实上,我们可以设置bean的作用域,如下:
<?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="car" class="logan.spring.study.autowire.Car" scope="prototype">
<property name="brand" value="Audi"></property>
<property name="price" value="3000000"></property>
</bean>
</beans>
package logan.spring.study.scope; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import logan.spring.study.autowire.Car; public class Main { public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml"); Car car1 = (Car) ctx.getBean("car");
Car car2 = (Car) ctx.getBean("car"); System.out.println(car1 == car2);
}
}
下面是输出结果
五月 20, 2017 4:42:00 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Sat May 20 16:42:00 CST 2017]; root of context hierarchy
五月 20, 2017 4:42:00 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-scope.xml]
false
使用prototype作用域,bean的实例就不是单例的了。每次获取Bean的时候返回的都是新的Bean。默认值是单例的。
Spring入门第八课的更多相关文章
- Spring入门第六课
XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean.需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式 ByType(根据类型自动装配):若I ...
- Spring入门第五课
集合属性 在Spring中可以通过一组内置的xml标签(如:<list>,<set>,<map>)来配置集合属性. 配置java.util.List类型的属性,需要 ...
- Spring入门第四课
注入参数详解:null值和级联属性 可以使用专用的<null/>元素标签为Bean的字符串或其他对象类型的属性注入null值. 和Struts,Hiberante等框架一样,Spring支 ...
- Spring入门第三课
属性注入 属性注入就是通过setter方法注入Bean的属性值或依赖的对象. 属性植入使用<property>元素,使用name属性指定Bean的属性名称,value属性或者<val ...
- Spring入门第十三课
通过FactoryBean来配置Bean package logan.spring.study.factoryBean; public class Car { private String brand ...
- Spring入门第十一课
IOC容器中Bean的生命周期 Spring IOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务. Spring IOC容器对Bean的生命周期进行管理的过 ...
- Spring入门第十课
Spring表达式语言:SpEL Spring表达式语言(简称SpEL)是一个支持运行时查询和操作对象图的强大的表达式语言. 语法类似于EL:SpEL使用#{...}作为定界符,所有在大括号中的字符都 ...
- Spring入门第七课
Bean之间的关系:继承和依赖. 继承Bean配置 Spring允许继承bean的配置,被继承的bean称为父bean,继承这个父bean的Bean称为子Bean. 子Bean从父Bean中继承配置, ...
- Spring入门第十七课
AOP编程 问题: 代码混乱: 越来越多的非业务需求(日志和验证等)加入后,原有的业务方法急剧膨胀,每个方法在处理核心逻辑的同事还必须兼顾其他多个关注点. 代码分散:以日志需求为例,只是为了满足这个单 ...
随机推荐
- Error -27728: Step download timeout (120 seconds)的解决方法(转)
LR中超时问题解决方法 超时错误在LoadRunner录制Web协议脚本回放时超时经常出现. 现象1:Action.c(16): Error -27728: Step download timeout ...
- 【BZOJ3251】树上三角形 暴力
[BZOJ3251]树上三角形 Description 给定一大小为n的有点权树,每次询问一对点(u,v),问是否能在u到v的简单路径上取三个点权,以这三个权值为边长构成一个三角形.同时还支持单点修改 ...
- java设计模式之迭代器模式
一.迭代器模式简介 迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 迭代器模式让我们能游走于聚合内的每一个元素,而又不暴露内部的表示.把游走的任务放在迭代器上,而不是 ...
- angularJs-未加载完成的页面显示混乱ng-bind
ng-bind 未初始化完成不加载数据,避免产生{{}}导致页面混乱 还是上边的例子,当前页面没有加载完成的时候,页面显示是不完整的会频繁的出现{{}}这样子的表达式语句,给用户的感觉很不和谐,所以使 ...
- POJ - 2031 Building a Space Station 【PRIME】
题目链接 http://poj.org/problem?id=2031 题意 给出N个球形的 个体 如果 两个个体 相互接触 或者 包含 那么 这两个个体之间就能够互相通达 现在给出若干个这样的个体 ...
- 【Leetcode-easy】String to Integer(atoi)
题目要求:字符串->整型 * 1. 首先需要丢弃字符串前面的空格. * 2. 然后可能有正负号(注意只取一个,如果有多个正负号,那么说这个字符串是无法转换的,返回0.比如测试用例里就有个“+-2 ...
- CSS相对|绝对(relative/absolute)定位
1.破坏性和包裹性 position:absolute与float:left,两者有两大共性:包裹性,破坏性. 包裹性 包裹性换种说法就是让元素inline-block化,例如一个div标签默认宽度是 ...
- 关于RabbitMQ简介
RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然在同步消息通讯的世界里有很多公开标准 ...
- line -1: Validation of SOAP-Encoded messages not supported
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=" ...
- 蓝天白云大草原风景PSD背景素材
蓝天白云大草原风景PSD源文件背景素材,蓝天白云,大草原,风景,背景素材,自然风景,草原景色,绿色清新背景 地址:http://www.huiyi8.com/psd/