Spring-----------------Singleton的使用与否
来源:互联网 发布:音频工作站软件参数 编辑:程序博客网 时间:2024/05/21 23:34
上篇文章已经谈了《Spring Framework 开发参考手册》的3.2.4小节——《Bean的标志符(id与name)》,现在继续3.2.5小节——《Singleton的使用与否》。
文档已经把Singleton说的很清楚了,这里也不废话了。主要是了解在Spring中,默认的部署方式是Singleton,没有特别需要的话,一般使用Singleton。
相关软件的配置可以参考《快速上手Spring--2.HelloWorld(1)》。
这里还是以HelloBean为例,这三个文件都放在包javamxj.spring.basic.singleton下。
· HelloBean
这里多加了属性i,用它得到随机数。
HelloBean.java
package javamxj.spring.basic.singleton;
public class HelloBean {
private String helloWorld;
private int i = (int) (100 * Math.random());
public HelloBean(String helloWorld) {
this.helloWorld = helloWorld;
}
public void sayHello() {
System.out.println(helloWorld);
System.out.println("输出随机整数: " + i);
}
}
· 配置文件
在这个Beans中,定义了两个bean,都指向HelloBean类,主要不同就是第二个bean指明了singleton="false",即不采用singleton部署方式。
bean.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="singletonBean" class="javamxj.spring.basic.singleton.HelloBean">
<constructor-arg>
<value>Hello! 这是singletonBean!</value>
</constructor-arg>
</bean>
<bean id="prototypeBean" class="javamxj.spring.basic.singleton.HelloBean"
singleton="false">
<constructor-arg>
<value>Hello! 这是prototypeBean! </value>
</constructor-arg>
</bean>
</beans>
· 测试程序:
分别取得singletonBean和prototypeBean各两个实例,然后输出随机数,最后比较同一个bean的两个实例的引用是否相同。
Main.java
package javamxj.spring.basic.singleton;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Main {
public static void main(String[] args) {
Resource res = new ClassPathResource("javamxj/spring/basic/singleton/bean.xml");
BeanFactory factory = new XmlBeanFactory(res);
HelloBean h1 = (HelloBean) factory.getBean("singletonBean");
h1.sayHello();
HelloBean h2 = (HelloBean) factory.getBean("singletonBean");
h2.sayHello();
System.out.println("h1==h2: " + (h1==h2));
System.out.println("");
HelloBean h3 = (HelloBean) factory.getBean("prototypeBean");
h3.sayHello();
HelloBean h4 = (HelloBean) factory.getBean("prototypeBean");
h4.sayHello();
System.out.println("h3==h4: " + (h3==h4));
}
}
·运行,控制台输出:
Hello! 这是singletonBean!
输出随机整数: 7
Hello! 这是singletonBean!
输出随机整数: 7
h1==h2: true
Hello! 这是prototypeBean!
输出随机整数: 95
Hello! 这是prototypeBean!
输出随机整数: 60
h3==h4: false
·这篇文章和上篇文章的源代码下载(不包含库文件):http://free.ys168.com/?javamxj Spring目录下面的SpringBasic.zip。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/javamxj/archive/2005/07/22/432006.aspx
- Spring-----------------Singleton的使用与否
- 快速上手Spring--6. Singleton的使用与否
- 快速上手Spring--6. Singleton的使用与否
- 快速上手Spring--6. Singleton的使用与否
- 快速上手Spring--6. Singleton的使用与否
- 快速上手Spring--6. Singleton的使用与否
- Spring笔记之六(IoC Singleton) 单例的使用与否
- 使用Spring Boot与否,初始化Spring应用的对比
- Spring中scope属性singleton和prototype使用的区别
- spring注入成功与否
- Spring Singleton的陷阱介绍
- Spring的Bean-singleton模式
- Singleton 的使用实现
- 使用 策略模式 控制用户的登录与否的行为
- Python中file.close()方法的使用与否
- JDBC插入数据使用索引与否的差别
- Spring singleton
- NoSQL的是与否
- Delphi中的Format函数详解
- 按键精灵QQ自由幻想后台加血挂机辅助 内存基址
- 自学ICE基础介绍
- 记中小企业管理软件的最佳实践者---中国知名品牌华软ZHRSOFT
- 企业为什么需要信息化
- Spring-----------------Singleton的使用与否
- CUDA学习——BMP文件格式
- 也没那么消极...
- 怎么让Ubuntu9.10记住授权
- 企业为什么要使用管理软件一
- 我的Flex学习笔记
- 企业为什么要使用管理软件二
- Java访问权限浅析
- WinCE EBOOT中的Boot Args与Driver Globals