spring 作用域之单例模式/原型模型
来源:互联网 发布:java怎么搭建服务器 编辑:程序博客网 时间:2024/04/29 08:48
1.配置文件
<?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="com.hxzy.model.Car" scope="singleton">
<property name="brand" value="bmw"></property>
<property name="address" value="天津"></property>
<property name="speed" value="220"></property>
</bean>
</beans>
2.Car类
package com.hxzy.model;
public class Car {
private String brand;
private String address;
private int speed;
public void setBrand(String brand) {
this.brand = brand;
}
public void setAddress(String address) {
this.address = address;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public Car() {
}
public Car(String brand, String address, int speed) {
super();
this.brand = brand;
this.address = address;
this.speed = speed;
}
@Override
public String toString() {
return "brand : " + brand + " address : " + address + " speed : " + speed;
}
}
3.测试类
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("spring_scope.xml");
Car car = (Car) ctx.getBean("car");
//System.out.println(car);
Car car2 = (Car) ctx.getBean("car");
System.out.println(car == car2);
ctx.close();
输出时结果为true,因为使用了单例模式获取car和car2
若改为使用 <bean id="car" class="com.hxzy.model.Car" scope="prototype">
则输出为false
- spring 作用域之单例模式/原型模型
- Spring-5 bean的作用范围---单例和原型
- spring bean单例模式改成原型模式
- 创建型模式之原型模式和单例模式
- 设计模式之原型模型
- spring注解设置单例和原型模式
- 架构设计二之原型模式、单例模式
- TypeScript设计模式之单例、建造者、原型
- TypeScript设计模式之单例、建造者、原型
- 设计模式-原型模式、单例模式
- Java设计模型之:原型模式
- 单例模式 和 原型模式
- Spring 设计模式之单例
- Spring 设计模式之单例
- Python的创建型设计模式之原型模式与单例模式
- java设计模式之创建模式(工厂模式,Builder,单例,原型)
- 关于spring单例,原型对象模式下犯的错
- spring 单例模式
- R连接MySQL数据库方法
- [root@localhost zhu]# yum install vim Loaded plugins: langpacks, presto, refresh-packagekit Adding e
- 堆排序 Heap Sort
- javascript创建对象常用方法
- AJAX学习笔记
- spring 作用域之单例模式/原型模型
- ns-2与TCP拥塞模拟 之五 ns-2中添加核心模块-TCPAgent子类
- 【笔试】55、实现吧字符串转化为数字(C的atoi函数)
- HDU 1950 Bridging signals(树状数组)
- UI 动画之CALayer+CAAnimation 之 CATransition
- 文本挖掘概念学习(一)
- UVALive 5874 - Social Holidaying(一般图匹配带花树)
- Git merge 合并分区详解
- point on C_4 语句