网站模板简易修改,单位网络建设的设计方案,太原建站网站模板,抖音视频排名优化为了帮助大家更深入的理解bean的作用域#xff0c;特意将BeanDefinition的双例支持留到本章节中#xff0c;创建Bean,相关Reader读取等逻辑都有所改动。 内容介绍 在Spring中#xff0c;Bean的作用域#xff08;Scope#xff09;定义了Bean的生命周期和可见性。包括单例和… 为了帮助大家更深入的理解bean的作用域特意将BeanDefinition的双例支持留到本章节中创建Bean,相关Reader读取等逻辑都有所改动。 内容介绍 在Spring中Bean的作用域Scope定义了Bean的生命周期和可见性。包括单例和原型在本章节中我们将为Bean添加多例的支持下面是Prototype作用域的几个特征介绍
1. 多例Prototype Bean的prototype作用域表示每次从容器中获取该Bean时都会创建一个新的实例。每个请求或依赖注入都会导致创建一个全新的Bean对象。
2. 不受Spring容器管理 prototype作用域的Bean实例不受Spring容器的管理容器在创建Bean之后不会再跟踪它。这意味着Spring容器不会负责释放或销毁prototype作用域的Bean。一旦Bean被创建并交给调用者调用者负责Bean的生命周期包括销毁。
3. 适用场景 prototype作用域适用于那些每次获取实例时都需要全新状态的Bean例如HTTP请求的处理器或线程池中的任务。这样可以确保每次获取的Bean都是独立的不会影响其他实例。 这里设计到设计模式中的原型模式形象地说可以将prototype作用域的Bean比喻为一个不断复制的模具。每次你需要一个新的Bean实例时Spring容器会根据模具再次制作一个全新的Bean。这个模具本身不受容器管理而是由你自己管理它的生命周期。这与singleton作用域的Bean不同后者是像一个单一的实例化对象由容器管理其生命周期。 prototype作用域 可见prototype类型的bean并不受spring容器的管理你需要自己负责销毁或释放这些Bean实例以防止内存泄漏。
代码分支
GitHub - yihuiaa/little-spring: 剖析Spring源码包括常见特性IOC、AOP、三级缓存等...剖析Spring源码包括常见特性IOC、AOP、三级缓存等... Contribute to yihuiaa/little-spring development by creating an account on GitHub.https://github.com/yihuiaa/little-spring
核心代码 BeanDefinition private String scope SCOPE_SINGLETON;private boolean singleton true;private boolean prototype false;public static String SCOPE_SINGLETON singleton;public static String SCOPE_PROTOTYPE prototype;public void setScope(String scope) {this.scope scope;this.singleton SCOPE_SINGLETON.equals(scope);this.prototype SCOPE_PROTOTYPE.equals(scope);}public boolean isSingleton() {return this.singleton;}public boolean isPrototype() {return this.prototype;}
AbstractAutowireCapableBeanFactory#doCreateBean()
... protected Object doCreateBean(String beanName, BeanDefinition beanDefinition) {Object bean null;try {bean createBeanInstance(beanDefinition);//为bean填充属性applyPropertyValues(beanName, bean, beanDefinition);//执行bean的初始化方法和BeanPostProcessor的前置和后置处理方法initializeBean(beanName, bean, beanDefinition);} catch (Exception e) {throw new BeansException(Instantiation of bean failed, e);}//注册有销毁方法的beanregisterDisposableBeanIfNecessary(beanName, bean, beanDefinition);if (beanDefinition.isSingleton()) {addSingleton(beanName, bean);}return bean;}
...
AbstractAutowireCapableBeanFactory#registerDisposableBeanIfNecessary() /*** 注册有销毁方法的bean即bean继承自DisposableBean或有自定义的销毁方法** param beanName* param bean* param beanDefinition*/protected void registerDisposableBeanIfNecessary(String beanName, Object bean, BeanDefinition beanDefinition) {//只有singleton类型bean会执行销毁方法if (beanDefinition.isSingleton()) {if (bean instanceof DisposableBean || StrUtil.isNotEmpty(beanDefinition.getDestroyMethodName())) {registerDisposableBean(beanName, new DisposableBeanAdapter(bean, beanName, beanDefinition));}}}测试
prototype-bean.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdbean idcar classbean.Car scopeprototypeproperty namename valueRolls-Royce//bean/beans
单元测试
public class PrototypeBeanTest {Testpublic void testPrototype() throws Exception {ClassPathXmlApplicationContext applicationContext new ClassPathXmlApplicationContext(classpath:prototype-bean.xml);Car car1 applicationContext.getBean(car, Car.class);Car car2 applicationContext.getBean(car, Car.class);System.out.println(car1 car2);;}
}测试结果
false
Process finished with exit code 0