Spring Boot 中的静态方法是没有办法直接获取到应用上下文中的 Bean 的。如果你想要在静态方法中使用其他 Service,有两种方法可以解决:
- 使用依赖注入的方式:
在你的 Util 类中定义一个 Service 属性,然后在 Util 类的构造函数中将 Service 实例注入进来。这样就可以在 Util 类的非静态方法中使用 Service 了。
- 使用 Spring 的 ApplicationContextAware 接口:
ApplicationContextAware 接口是 Spring 提供的一个接口,它允许某个 Bean 获取应用上下文中的 App