Spring中Bean的使用
2016-05-22 22:35
555 查看
1:和变量一样,bean也有作用域,spring中我们可以为bean指定作用域:<bean id="" class="" scope="....">
2:作用域的种类
singleton:单例模式,在spring中只有一个实例,无论多少个Bean引用,始终都会指向同一个对象。这也是spring默认的作用域。
prototype:原型模式,spring容器会为每一个引用创建一个新实例。
request:每一个HTTP请求都会创建一个新的bean,但仅仅在当前的request中有效。
session:每一个HTTP Session都会创建一个新的bean,但仅仅在当前HTTP Session中有效。
global Session:在一个全局的HTTP Session中,容器会创建实例,但仅仅在当前范围有效。
实际开发中,单例和原型用的比较多。
3:演示单例模式作用域
bean定义
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
public class TopicService
{
public void addTopic()
{
System.out.println("add topic");
}
}
xml配置bean
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
<?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="topServiceID" class="com.canyugan.scope.TopicService"></bean>
</beans>
测试单例模式下是否引用的是同一个实例:
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
@Test
public void demo1()
{
String xmlpath="com/canyugan/scope/beans.xml";
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlpath);
TopicService topicService1=applicationContext.getBean("topServiceID",TopicService.class);
System.out.println(topicService1);
TopicService topicService2=applicationContext.getBean("topServiceID",TopicService.class);
System.out.println(topicService2);
}
2:作用域的种类
singleton:单例模式,在spring中只有一个实例,无论多少个Bean引用,始终都会指向同一个对象。这也是spring默认的作用域。
prototype:原型模式,spring容器会为每一个引用创建一个新实例。
request:每一个HTTP请求都会创建一个新的bean,但仅仅在当前的request中有效。
session:每一个HTTP Session都会创建一个新的bean,但仅仅在当前HTTP Session中有效。
global Session:在一个全局的HTTP Session中,容器会创建实例,但仅仅在当前范围有效。
实际开发中,单例和原型用的比较多。
3:演示单例模式作用域
bean定义
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
public class TopicService
{
public void addTopic()
{
System.out.println("add topic");
}
}
xml配置bean
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
<?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="topServiceID" class="com.canyugan.scope.TopicService"></bean>
</beans>
测试单例模式下是否引用的是同一个实例:
[java] view
plain copy
print?
![](https://code.csdn.net/assets/CODE_ico.png)
@Test
public void demo1()
{
String xmlpath="com/canyugan/scope/beans.xml";
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlpath);
TopicService topicService1=applicationContext.getBean("topServiceID",TopicService.class);
System.out.println(topicService1);
TopicService topicService2=applicationContext.getBean("topServiceID",TopicService.class);
System.out.println(topicService2);
}
相关文章推荐
- spring mvc 异常统一处理方式
- 用遗传算法走迷宫 [Java语言实现]
- Java中获取路径的方法
- Java实现复制一个目录下的所有文件
- java之装饰者模式
- java程序使用JDBC接口连接数据库
- Struts 2 的stream result用法
- 市长信箱邮件查询服务: 使用SpringBoot搭建基础
- spring mvc 关于乱码的几种解决方式
- java基础第五天
- Java基础知识总结(绝对经典)
- 利用for循环打印实心棱形和空心棱形
- spring <context:component-scan />及<mvc:annotation-driven />使用说明
- java中静态代码块
- Java的BoxLayout的水平支撑组件和垂直支撑组件
- Java大牛养成记(8)——何为servlet?
- Java.Swing.button绑定快捷键的3种办法
- Java 多线程总结(一)
- springMVC(12)------表单标签的使用
- 在eclipse上配置maven