玩转Spring各种作用域Bean Scope及源码分析( 三 )


【玩转Spring各种作用域Bean Scope及源码分析】3.2 查找web作用域bean
public abstract class AbstractBeanFactory {protected <T> T doGetBean(...) {// ...// 判断是否是单例if (mbd.isSingleton()) {// ...}// 判断是否是原型else if (mbd.isPrototype()) {Object prototypeInstance = null;try {// 不存在什么缓存池,直接创建bean实例返回prototypeInstance = createBean(beanName, mbd, args);}}// 其它作用域bean,如上面的web作用域else {String scopeName = mbd.getScope();Scope scope = this.scopes.get(scopeName);if (scope == null) {throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");}try {// 通过具体Scope的实现类获取bean对象Object scopedInstance = scope.get(beanName, () -> {beforePrototypeCreation(beanName);try {// 首次都还是会创建return createBean(beanName, mbd, args);}});}}}// ...}}总结:Spring Scope Bean是Spring框架中用于管理Bean的作用域的机制,它定义了Bean的生命周期和实例化策略 。通过合理地选择Bean的作用域 , 可以优化应用的性能和资源利用率 。




推荐阅读