SoFunction
Updated on 2025-03-09

SpringBoot uses quartz, inject feignClient, client is null problem

SpringBoot uses quartz, inject feignClient, client is null

Use @autowired, live constructor injection, break point to view the injected feigncliet as null, search for some blogs on the Internet, the general reason is that quartz starts inject some classes into it through reflection, and reference this class at startup has not been initialized yet. Here is the solution:

Use spring to obtain beans by filename provided by

Inject in

@Component
public class SpringUtil implements ApplicationContextAware {
 
    private static ApplicationContext applicationContext;
 
    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        if ( == null) {
             = applicationContext;
        }
    }
 
    // Get Bean by name.    @NotNull
    public static Object getBean(String name) {
        return (name);
    }
 
    // Get Bean through class.    @NotNull
    public static <T> T getBean(Class<T> clazz) {
        return (clazz);
    }
 
    // Return the specified bean via name and Clazz    @NotNull
    public static <T> T getBean(String name, Class<T> clazz) {
        return (name, clazz);
    }
}

Loading classes through class names when using

Just use normally

CommunicationClient communicationClient = ();
 
// Call the method normally();
 

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.