SpringBoot中的静态方法如何获取bean

目录

SpringBoot中的静态方法获取bean的三种方案

方式一 注解@PostConstruct

方式二 启动类ApplicationContext

方式三 手动注入ApplicationContext


前言:在开发过程中,当在静态方法内调用bean的时候会遇到以下类型错误,均是使用不当造成的

ps:不看错误可以直接跳转标题“SpringBoot中的静态方法获取bean的三种方案

错误1:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderDetailInfoController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderDetailServiceFactory': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'WJOrderDetailService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopInfoHelper': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'wareInfoHelper': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'wareButtonHelper' defined in URL [jar:file:xxx/WEB-INF/lib/xxx-1.0-SNAPSHOT.jar!/com/xxx/mti/order/web/service/helpers/ware/WareButtonHelper.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: @Resource annotation is not supported on static fields
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(Com

  • 39
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值