spingcloud项目中,获取feign的bean失败

我的项目用到了quartz定时器,需要定时触发一些业务逻辑,这些逻辑当中又要通过feign用到websocket模块的接口,但是当定时器触发之后,找到那个fein之后,一调用里面的函数就报错,schedulerexception:job threw an unhandled exception,最开始我是用的autower注解注入,然后又试了springcontextholder.getbean都试了,还是不行

应该是获取bean的方式不对,你的详细代码和报错能贴出来吗?
springcontextholder这个东西有值吗?

是否没有加上feign发现注解 ,或者报错信息给得多点

确定是和定时器有关系么?用单元测试调一下feign的方法看看

你的启动类加上了Feign的注册发现么?

用test 分别单独测试 feign的接口和定时器任务;
信息详细一点

getBean的方式获取不到,说明你的类并没有交给spring容器进行管理,检查类上是否加了@Controller、@Service、@Component等相关注解

可以参考 :https://blog.csdn.net/xt314159/article/details/108345027

使用 hutool SpringUtil试下 能拿到吗


import cn.hutool.extra.spring.SpringUtil;

@Autowired
private FeignWebsocketService feignWebsocketService;

SpringUtil.getBean("feignWebsocketService")

getBean的方式获取不到,说明你的类并没有交给spring容器进行管理

这个应该是定时器中没有拿到对象造成的,你可以试试直接通过new的方式创建一个对象出来,然后在调用方法看看

看一下你的修饰符,是不是私有的,我遇到过相同的问题,我记得应该是我的修饰符出问题了

创建工具类SpringContextJobUtil,实现ApplicationContextAware接口,此工具类会在spring容器启动后,自动加载,使用其提供的getBean方法获取想要的bean即可

@Component
public class SpringContextJobUtil implements ApplicationContextAware  {
 
    private static ApplicationContext context;
 
    @Override
    @SuppressWarnings("static-access" )
    public void setApplicationContext(ApplicationContext contex)
            throws BeansException {
        // TODO Auto-generated method stub
        this.context = contex;
    }
    public static Object getBean(String beanName){
        return context.getBean(beanName);
    }
 
    public static String getMessage(String key){
        return context.getMessage(key, null, Locale.getDefault());
    }
}

在job内直接调用静态方法

@Autowired
private FeignWebsocketService feignWebsocketService;
// 此段代码为获取Bean
feignWebsocketService= (FeignWebsocketService) SpringContextJobUtil.getBean("feignWebsocketService");

定时任务里面尝试注入其他Service Bean看看能不能成功,如果不能就是Quartz调度Bean没有被Spring管理

给个最小单元复现代码

1、检查下quartz的版本和你的spring boot是否兼容

把你的feign类发的看下撒
有在启动类上加@EnableFeignClients注解吗