首页 Java java教程 applicationContextAware接口的setApplicationContext方法执行问题,获取Spring bean失败

applicationContextAware接口的setApplicationContext方法执行问题,获取Spring bean失败

Nov 23, 2024 am 07:53 AM

在这篇文章中我想分享我最近遇到的一个问题,我想您可能会感兴趣。

出了什么问题?

测试环境和生产环境的源代码是相同的,本地和测试环境都运行良好,只有生产环境运行的服务类的NPE加载失败,这是为了新的需求而添加的。该类继承自Customize包(自研工具包)的接口。

项目结构

The setApplicationContext method execution issue of the applicationContextAware interface and failed to get Spring beans

Customize 的设计依赖于 Spring 来管理 API 和服务。通过Spring自动扫描,初始化类时会加载ApiEnhancer和ServiceEnhancer的公共类,并通过ApplicationContext获取实例。调试发现加载它们时,ApplicationContext为null,还没有初始化。是在setApplicationContext方法中通过实现ApplicationContextAware来初始化的,所以推测setApplicationContext方法还没有被执行。

在ApplicationContextProvider类的加载过程中,静态方法在初始化阶段被加载到方法区中。然而,当使用ApplicationContext获取bean的实例时,静态方法是直接通过类名调用的。只要API是在ApplicationContextProvider在堆中分配内存并实例化之前创建的,就不会调用setApplicationContext方法进行初始化。

applicationContextProvider使用注解@Component,Api使用注解@RestController,两个类在同一个路径下。然而,当 Spring 扫描并加载它们时,没有特定的顺序,这意味着这两个类中的每一个都可能在另一个类之前创建。在生产环境中,Api是先于applicationContextProvider创建的,导致通过类名直接调用静态方法获取bean时,applicationContext没有被初始化。

解决方案

如果没有执行ApplicationContextAware接口的实现类的setApplicationContext方法,首先检查该实现类是否设置了延迟加载或者项目是否配置了全局延迟加载。

在这个项目中,问题是由ApiEnhancer和ApplicationContextAware接口的实现类的创建顺序引起的,而Api类只是一个常规的RESTful API,处理业务逻辑,可以在前端调用时加载-结束页。因此,改为延迟加载解决了问题。

建议在加载顺序方面尽量减少类之间的依赖关系。如果不可避免,可以使用延迟加载,或者使用@DependsOn、@Order、@Priority等注解来控制bean的加载顺序


为什么生产环境和测试环境的创建顺序不同?

我们来调试一下Spring的扫描过程。

从ClassPathBeanDeterminationScanner的scan方法开始。

The setApplicationContext method execution issue of the applicationContextAware interface and failed to get Spring beans

doScan方法

The setApplicationContext method execution issue of the applicationContextAware interface and failed to get Spring beans

ClassPathScanningCandidateComponentProvider 的 scanCandidateComponents 方法

The setApplicationContext method execution issue of the applicationContextAware interface and failed to get Spring beans

PathMatchingResourcePatternResolver 的 findAllClassPathResources 方法

The setApplicationContext method execution issue of the applicationContextAware interface and failed to get Spring beans

doFindPathMatchingJarResources 方法

The setApplicationContext method execution issue of the applicationContextAware interface and failed to get Spring beans

JarFile是Java标准库中java.util.jar包下的一个类,它继承并扩展了ZipFile。 jarFile.entries() 返回一个名为 JarEntryIterator 的迭代器。

The setApplicationContext method execution issue of the applicationContextAware interface and failed to get Spring beans

JarExitIterator 是 迭代器模式 的实现,它迭代 JarFile 父类的条目。

The setApplicationContext method execution issue of the applicationContextAware interface and failed to get Spring beans

ZipFile 的 Entry 方法返回一个名为 ZipExitIterator 的迭代器。

The setApplicationContext method execution issue of the applicationContextAware interface and failed to get Spring beans

ZipExitIterator 的 nextElement 方法调用 next 方法,后者又调用 getNextEntry 方法。

The setApplicationContext method execution issue of the applicationContextAware interface and failed to get Spring beans

getNextEntry 是一个原生方法。

The setApplicationContext method execution issue of the applicationContextAware interface and failed to get Spring beans

native 方法是用非 Java 语言实现的,并在 Java 虚拟机内调用来实现底层功能,这可能会因环境(操作系统或 JDK 版本)而异。 JAR包本身没有顺序,所以实际的遍历顺序可能会根据不同的JAR打包工具和环境而有所不同。

以上是applicationContextAware接口的setApplicationContext方法执行问题,获取Spring bean失败的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

公司安全软件导致应用无法运行?如何排查和解决? 公司安全软件导致应用无法运行?如何排查和解决? Apr 19, 2025 pm 04:51 PM

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

如何使用MapStruct简化系统对接中的字段映射问题? 如何使用MapStruct简化系统对接中的字段映射问题? Apr 19, 2025 pm 06:21 PM

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

如何将姓名转换为数字以实现排序并保持群组中的一致性? 如何将姓名转换为数字以实现排序并保持群组中的一致性? Apr 19, 2025 pm 11:30 PM

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

如何优雅地获取实体类变量名构建数据库查询条件? 如何优雅地获取实体类变量名构建数据库查询条件? Apr 19, 2025 pm 11:42 PM

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

IntelliJ IDEA是如何在不输出日志的情况下识别Spring Boot项目的端口号的? IntelliJ IDEA是如何在不输出日志的情况下识别Spring Boot项目的端口号的? Apr 19, 2025 pm 11:45 PM

在使用IntelliJIDEAUltimate版本启动Spring...

Java对象如何安全地转换为数组? Java对象如何安全地转换为数组? Apr 19, 2025 pm 11:33 PM

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

电商平台SKU和SPU数据库设计:如何兼顾用户自定义属性和无属性商品? 电商平台SKU和SPU数据库设计:如何兼顾用户自定义属性和无属性商品? Apr 19, 2025 pm 11:27 PM

电商平台SKU和SPU表设计详解本文将探讨电商平台中SKU和SPU的数据库设计问题,特别是如何处理用户自定义销售属...

使用TKMyBatis进行数据库查询时,如何优雅地获取实体类变量名构建查询条件? 使用TKMyBatis进行数据库查询时,如何优雅地获取实体类变量名构建查询条件? Apr 19, 2025 pm 09:51 PM

在使用TKMyBatis进行数据库查询时,如何优雅地获取实体类变量名以构建查询条件,是一个常见的难题。本文将针...

See all articles