Let’s first look at the error message
Description:
Field businessFeignClient in com.ysc.service.BusinessConfigService required a bean of type 'com.ysc.feignclient.BusinessFeignClient' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.ysc.feignclient.BusinessFeignClient' in your configuration.
Let’s take another look at feign’s configuration information
@SpringBootApplication(
scanBasePackages = "com.ysc",
exclude = {
DataSourceAutoConfiguration.class,
ThymeleafAutoConfiguration.class
})
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
On the surface, there is no problem with the configuration. Let’s analyze the specific causes of the problem
The annotation @ enablefeignclients conflicts with @ componentscan. Both annotations will search for beans injected into the specified directory@ Enablefeignclients introduces the feignclientsregister class to load spring bean resources
The registerfeignclients method in feignclientsregister obtains the basepackage attribute value in the @ enablefeignclients annotation and injects it. If both annotations are used, @ enablefeignclients will overwrite the directory specified in @ componentscan to restore to the default directory
How to solve this problem:
1. Feignclient can be placed in the same level directory as the application startup class
2. You can specify the bean directory through the clients property in @ enablefeignclients
@EnableFeignClients(clients = {
BusinessFeignClient.class
})