背景工具类一般都是静态方法静态方法只能访问静态属性。所以我们需要静态注入类或者属性。坑如果你使用这样的注入方式的话都是null注入不进去。Autowired private static TestService testService; Resource private static TestService testService; Value(${key}) private static String key;解决办法1PostConstruct方式实现Component public class TestUtil { Autowired private static TestService testService; private static TestUtil testUtils; PostConstruct public void init() { testUtils this; testUtils.testService this.testService; } }PostConstruct 注解的方法在加载类的构造函数之后执行也就是在加载了构造函数之后执行init方法(PreDestroy 注解定义容器销毁之前的所做的操作)这种方式和在xml中配置 init-method和 destory-method方法差不多定义spring 容器在初始化bean 和容器销毁之前的所做的操作2set方法注入实现Component public class TestUtil { private static TestService testService; private static String key; Value({key}) public void setTestService(String key) { TestUtil.key key; } Autowired public void setTestService(TestService testService) { TestUtil.testService this.testService; } }3MethodInvokingFactoryBean通过MethodInvokingFactoryBean工厂Bean可以将指定方法返回值注入成为目标Bean的属性值MethodInvokingFactoryBean用来获得指定方法的返回值该方法可以是静态方法 也可以是实例方法。 获得的方法返回值既可以被注入到指定Bean实例的指定属性也可以直接定义成Bean实例。Java实体类中是这样的private static String decryptToken; public static void setDecryptToken(String decryptToken) { DecryptUtil.decryptToken decryptToken; }xml中是这样的bean idconfigIdStatic classorg.springframework.beans.factory.config.MethodInvokingFactoryBean property namestaticMethod valuecom.....setDecryptToken/ property namearguments value${decryptToken}/ /bean如果arguments 是多个采用 List 赋值。bean idconfigIdStatic classorg.springframework.beans.factory.config.MethodInvokingFactoryBean property namestaticMethod valuecom....setDecryptToken/ property namearguments list valuedecryptToken/value /list /property /bean参考https://segmentfault.com/a/1190000019844427utm_sourcetag-newesthttps://www.sojson.com/blog/157.htmlhttps://www.cxyzjd.com/article/weixin_42608550/97675350