千家信息网

springboot怎么完善上下文加载器

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇内容介绍了"springboot怎么完善上下文加载器"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成
千家信息网最后更新 2026年11月01日springboot怎么完善上下文加载器

本篇内容介绍了"springboot怎么完善上下文加载器"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1. prepareContext

源码:

``` private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {    //注入环境属性    context.setEnvironment(environment);    //上下文后置处理    this.postProcessApplicationContext(context);    //完善初始化类的属性    this.applyInitializers(context);    //发送监听事件    listeners.contextPrepared(context);    //日志    if (this.logStartupInfo) {        this.logStartupInfo(context.getParent() == null);        this.logStartupProfileInfo(context);    }    //注册传入的配置参数为bean,这里将传入的参数封装成applicationArguments,内部类似命令行    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();    beanFactory.registerSingleton("springApplicationArguments", applicationArguments);    //banner打印    if (printedBanner != null) {        beanFactory.registerSingleton("springBootBanner", printedBanner);    }    //这里默认情况下bean定义不允许重复    if (beanFactory instanceof DefaultListableBeanFactory) {        ((DefaultListableBeanFactory)beanFactory).setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);    }    //默认不开启延迟加载    if (this.lazyInitialization) {        context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());    }    //获取全部的资源    //这里获取了启动类的资源和 当前SpringApplication中的source资源。    //到目前来说实际上只有启动类资源    Set sources = this.getAllSources();    Assert.notEmpty(sources, "Sources must not be empty");    this.load(context, sources.toArray(new Object[0]));    listeners.contextLoaded(context);}```

老样子进行逐个分析。但看这个方法并不复杂,整体上对上下文和工厂类进行配置的完善。

1.1 postProcessApplicationContext 方法

源码:

   protected void postProcessApplicationContext(ConfigurableApplicationContext context) {        //  是否自定义bean名称生成类        if (this.beanNameGenerator != null) {            context.getBeanFactory().registerSingleton("org.springframework.context.annotation.internalConfigurationBeanNameGenerator", this.beanNameGenerator);        }        //是否指定类加载器        if (this.resourceLoader != null) {            if (context instanceof GenericApplicationContext) {                ((GenericApplicationContext)context).setResourceLoader(this.resourceLoader);            }            if (context instanceof DefaultResourceLoader) {                ((DefaultResourceLoader)context).setClassLoader(this.resourceLoader.getClassLoader());            }        }                //是否添加数据转换器         //在初始化环境对象的时候也有用到,这里可以直接通过 context.getEnvironment().getConversionService()获取到        if (this.addConversionService) {            context.getBeanFactory().setConversionService(ApplicationConversionService.getSharedInstance());        }    }

1.2 applyInitializers

完善与ApplicationContextInitializer接口相关的对象属性。这些对象在this.initializers中,早在SpringApplication初始化的时候就已经加载。通过已经初始化好的上下文对相关类进行完善。调用接口的initialize方法。

1.3 load

源码:

        protected void load(ApplicationContext context, Object[] sources) {                if (logger.isDebugEnabled()) {                        logger.debug("Loading source " + StringUtils.arrayToCommaDelimitedString(sources));                }                //构建一个bean定义的加载器                BeanDefinitionLoader loader = createBeanDefinitionLoader(getBeanDefinitionRegistry(context), sources);                if (this.beanNameGenerator != null) {                        loader.setBeanNameGenerator(this.beanNameGenerator);                }                if (this.resourceLoader != null) {                        loader.setResourceLoader(this.resourceLoader);                }                if (this.environment != null) {                        loader.setEnvironment(this.environment);                }                //将资源加载成bean                loader.load();        }                void load() {                for (Object source : this.sources) {                        load(source);                }        }    //按资源类型分别进行加载,        private void load(Object source) {                Assert.notNull(source, "Source must not be null");                if (source instanceof Class) {                        load((Class) source);                        return;                }                if (source instanceof Resource) {                        load((Resource) source);                        return;                }                if (source instanceof Package) {                        load((Package) source);                        return;                }                if (source instanceof CharSequence) {                        load((CharSequence) source);                        return;                }                throw new IllegalArgumentException("Invalid source type " + source.getClass());        }

主要加载了SpringApplication内初始化的资源,包括我们的启动类xxApplication将会被注册成bean。

"springboot怎么完善上下文加载器"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

资源 上下 上下文 对象 属性 方法 源码 内容 参数 实际 情况 接口 时候 更多 环境 知识 配置 复杂 实用 学有所成 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 科技互联网 喔噻互联网科技 致聘互联网科技 壹账通互联网科技 兼蜜互联网科技 镁科技互联网资讯平台 互联网实体化科技化 成都玖崟互联网科技有限公司乐商圈怎么样 科技互联网的估值 互联网科技教育视频 厦门比较好的科技互联网公司 千方科技属于互联网企业吗 杭州壁岐互联网科技有限公司 特约风豹互联网科技为什么扣费 神州科技互联网有限公司 萌小萌互联网科技河南有限公司 广西科技产业互联网是什么 北京天晋互联网科技有限公司 优雇互联网科技有限公司 互联网 科技节活动 微视频大赛 什么是科技互联网 互联网科技网站 互联网金融和金融科技 卫星互联网科技股票 哈尔滨新华互联网科技学院 深圳艾果互联网通讯科技 云鲸互联网络科技有限公司 北京互联网科技类企业分布 深圳互联网科技园地址 互联网科技创业有什么好项目
0