Bean的作用域

网友投稿 711 2022-05-29

Bean的作用域

默认情况下,所有Spring Bean都是单例的(整个Spring应用中,Bean的实例只有一个)。可以再元素中添加scope属性来配置Spring Bean的作用范围。

scope作用域范围:

Bean的作用域

注意:在以上 6 种 Bean 作用域中,除了 singleton 和 prototype 可以直接在常规的 Spring IoC 容器(例如 ClassPathXmlApplicationContext)中使用外,剩下的都只能在基于 Web 的 ApplicationContext 实现(例如 XmlWebApplicationContext)中才能使用,否则就会抛出一个 IllegalStateException 的异常。

单例模式singleton

singleton是Spring Ioc容器默认的作用域,此时容器中只会存在一个共享的Bean实例。这个Bean实例会被存储在高速缓存中,所有对于这个Bean的请求和引用,只要id与这个Bean定义相匹配,都会返回这个缓存中的对象实例。

在 Spring 配置文件中,可以使用 元素的 scope 属性,将 Bean 的作用域定义成 singleton,其配置方式如下所示:(不声明scope属性默认也是singleton)

代码示例:

实体类

package org.demo5; public class SingletonBean { private String str; public void setStr(String str) { this.str = str; } }

xml配置文件

main方法

package org.demo5; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { //private static final Log LOGGER= LogFactory.getLog(MainApp.class); public static void main(String[] args) { //创建bean工厂 ApplicationContext context=new ClassPathXmlApplicationContext("demo5beans.xml"); //取bean SingletonBean sBean1=context.getBean("singletonBean",SingletonBean.class); SingletonBean sBean2=context.getBean("singletonBean",SingletonBean.class); System.out.println(sBean1); System.out.println(sBean2); } }

运行结果:

org.demo5.SingletonBean@696da30b org.demo5.SingletonBean@696da30b

从控制台的输出可以看出,两次获得的 Bean 实例的地址完全一样,这说明 IoC 容器只创建了一个 singletonBean 实例。由于 singleton 是 Spring IoC 容器的默认作用域,因此即使省略 scope 属性,控制台的输出结果也一样的。

原型模式prototype

当Bean定义的作用域为prototype,Spring容器会在每次请求该Bean时,都会创建一个新的Bean实例。

代码示例:

实体类

package org.demo5; public class PrototypeBean { private String str; public void setStr(String str) { this.str = str; } }

XML配置文件

main方法类

package org.demo5;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp { //private static final Log LOGGER= LogFactory.getLog(MainApp.class); public static void main(String[] args) { //创建bean工厂 ApplicationContext context=new ClassPathXmlApplicationContext("demo5beans.xml"); //singleton SingletonBean sBean1=context.getBean("singletonBean",SingletonBean.class); SingletonBean sBean2=context.getBean("singletonBean",SingletonBean.class); System.out.println(sBean1); System.out.println(sBean2); //prototype PrototypeBean pBean1=context.getBean("prototypeBean",PrototypeBean.class); PrototypeBean pBean2=context.getBean("prototypeBean",PrototypeBean.class); System.out.println(pBean1); System.out.println(pBean2); }}

运行结果

org.demo5.PrototypeBean@be35cd9 org.demo5.PrototypeBean@4944252c

从运行结果可以看出,两次输出的内容并不相同,这说明在 prototype 作用域下,Spring 容器创建了两个不同的 prototypeBean 实例。

Spring web前端

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:图解进程线程、互斥锁与信号量-看完不懂你来打我
下一篇:运行时数据区中包含哪些区域?哪些线程共享?哪些线程独享?
相关文章