Spring --- Bean的获取、实例化与作用域
传统应用程序可以通过new和反射方式进行实例化Bean。而Spring IOC容器则需要根据Bean定义里的配置元数据使用BeanFactory的反射机制来创建Bean。
一、BeanFactory
1.1 ApplicationContext是BeanFactory接口的扩展接口。
1.2 BeanFactory采用延迟加载的方案,只有真正在getBean时才会实例化Bean。
1.3 在开发中我们一般使用的是ApplicationContext,具体实现类有:
(1) ClassPathXmlApplicationContext --- 根据类路径获取
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Bean1 bean1 = (Bean1) applicationContext.getBean("bean1");
bean1.show();
(2) FileSystemXmlApplicationContext --- 根据文件路径获取
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src/applicationContext.xml");
Bean1 bean1 = (Bean1) applicationContext.getBean("bean1");
bean1.show();
1.4 ApplicationContext会在配置文件加载时,就会初始化Bean,并且ApplicationContext提供了不同的应用层的Context实现。例如在web开发中可以使用WebApplicationContext。
二、Spring Bean的三种实例化方式
2.1 使用构造器实例化Bean
构造器实例化Bean是最简单的方式,Spring IoC容器既能使用默认空构造器,也能使用有参构造器两种方式创建Bean。
<bean name="bean1" class="com.spring.bean.Bean1" scope="prototype"></bean>
2.2 使用静态工厂实例化Bean
使用静态工厂的方式除了指定必须的class属性,还要指定factory-method属性来指定实例化Bean的方法,而且使用静态工厂方法也允许指定方法参数,Spring IoC容器将调用此属性指定的方法来获取Bean。
- ApplicationContext配置信息
<!-- 静态工厂方法 -->
<bean name="bean2" class="com.spring.bean.Bean2Factory" factory-method="createBean2"></bean>
- Bean2Factory静态工厂类
public class Bean2Factory {
public static Bean2 createBean2() {
return new Bean2();
}
}
- Test类
// 静态工厂方法获取Bean
@Test
public void test4() {
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src/applicationContext.xml");
Bean2 bean2 = (Bean2) applicationContext.getBean("bean2");
bean2.show();
}
2.3 使用实例化工厂实例化Bean
使用实例工厂方式不能指定class属性,此时必须使用factory-bean属性来指定工厂Bean,factory-method属性指定实例化Bean的方法,而且使用实例工厂方法允许指定方法参数,方式和使用构造器方式一样。
- ApplicationContext配置信息
<!-- 实例工厂方法 -->
<bean name="bean3Factory" class="com.spring.bean.Bean3Factory"></bean>
<bean name="bean3" factory-bean="bean3Factory" factory-method="createBean3"></bean>
- Bean3Factory实例化工厂类
public class Bean3Factory {
public Bean3 createBean3() {
return new Bean3();
}
}
- Test类
// 实例工厂方法获取Bean
@Test
public void test5() {
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src/applicationContext.xml");
Bean3 bean3 = (Bean3) applicationContext.getBean("bean3");
bean3.show();
}
三、Spring Bean的作用域
在Bean的声明时有一个scope属性,它是用于描述bean的作用域。可取值有:
- singleton : 单例
- prototype : 多例
- request : 用在web开发中
- session : 用在web开发中
3.1 singleton:单例模式
Spring IoC容器中只会存在一个共享的Bean实例,无论有多少个Bean引用它,始终指向同一对象。Singleton作用域是Spring中的缺省作用域,也可以显示的将Bean定义为singleton模式。
3.2 prototype:多例模式
每次通过Spring容器获取prototype定义的bean时,容器都将创建一个新的Bean实例,每个Bean实例都有自己的属性和状态,而singleton全局只有一个对象。根据经验,对有状态的bean使用prototype作用域,而对无状态的bean使用singleton作用域。
3.3 request:在一次Http请求中,容器会返回该Bean的同一实例。而对不同的Http请求则会产生新的Bean,而且该bean仅在当前Http Request内有效。
3.4
session:在一次Http Session中,容器会返回该Bean的同一实例。而对不同的Session请求则会创建新的实例,该bean实例仅在当前Session内有效。
上一篇: 图解ThinkPHP5框架(四):一张图彻底吃透框架配置(终结版)
下一篇: Vue快速入门
推荐阅读
-
Spring启动后获取所有拥有特定注解的Bean实例代码
-
JSP 中Spring Bean 的作用域详解
-
Python3.5基础之函数的定义与使用实例详解【参数、作用域、递归、重载等】
-
spring源码分析系列5:ApplicationContext的初始化与Bean生命周期
-
Spring启动后获取所有拥有特定注解的Bean实例代码
-
02Spring基于xml的IOC配置--实例化Bean的三种方式
-
Spring 框架基础(02):Bean的生命周期,作用域,装配总结
-
spring-bean的作用域
-
Python3.5基础之函数的定义与使用实例详解【参数、作用域、递归、重载等】
-
Spring中bean的作用域与生命周期