报错信息:
Error creating bean with name 'residentController': Unsatisfied dependency expressed through field 'signatureUtils'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'signatureUtils' defined in file [D:\Code\p1\ybdjk_new_hlwyy\ybdjk_new_hlwyy\ruoyi-common\target\classes\com\ruoyi\common\gateway\SignatureUtils.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.ruoyi.common.gateway.SignatureUtils]: Constructor threw exception; nested exception is java.lang.UnsupportedOperationException: This is a utility class and cannot be instantiated
使用springboot构建项目时,启动项目出现报错“residentController”的bean时出错:通过字段“signatureUtils”表示不满意的依赖关系;
我在 residentController 这个类引入的时候,用了 @Autowired
这就需要了解一下 @Autowired 注解的使用前提:
1.获取从spring容器管理的bean,bean中使用的@autowired注解生效,@autowired的属性能从spring容器中注入,是在bean实例化后通过setter方法注入。
2.new出来的对象,不受spring管理,所以使用了@autowired的属性失效为空
3.@confoguration类加载顺序高于被容器管理的类,不能用@autowired,因为还没注入spring容器中,如需要使用spring容器中的bean,在@bean方法的参数定义,会自动注入
总之,我要调用的类里面 signatureUtils 有很多静态方法,直接用signatureUtils.方法名的方式 就可以,不需要 @Autowired ;
根本原因:工具类使用@Autowired无法注入bean的原因大多是因为使用了静态方法,相应的,使用@Autowired注入的对象也应该被static修饰,这样spring就无法正常注入bean
所以,我的解决方式是 删掉代码中的 @Autowired,,,,, 然后再其项目时就解决了。