@EnableFeignClients 和@FeignClient 在自动装配时失败 'FeignContext' NoSuchBeanException
@EnableFeignClients and @FeignClient fail on Autowiring 'FeignContext' NoSuchBeanException
我正在编写的微服务需要与我们平台中的其他微服务进行通信。在那次尝试中,我们的理想解决方案是 Spring Cloud Netflix Feign,实施 @FeignClient
.
但是,当我尝试 @Autowired ReviewProvider
:
时遇到以下异常
异常(原因)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.netflix.feign.FeignContext' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:155)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
ReviewProvider.java
@FeignClient("http://metadata-reviews")
public interface ReviewProvider {
@RequestMapping(path = "sessions", method = POST)
ReviewSessionDTO createSession();
}
ReviewProvider.java
@RunWith(SpringRunner.class)
@ActiveProfiles(INTEGRATION)
@ContextConfiguration(classes = AppEntry.class)
@AutoConfigureTestDatabase(replace = Replace.NONE)
@DataJpaTest
public class ReviewProviderTest {
@Autowired
private ReviewProvider provider;
private Class<? extends ReviewProvider> providerClass;
@Before
public void setup() {
providerClass = provider.getClass();
}
@Test
public void classAnnotations() {
assertTrue(providerClass.isAnnotationPresent(FeignClient.class));
assertEquals("http://metadata-reviews", providerClass.getAnnotation(FeignClient.class).value());
}
@Test
public void createSession() throws Exception {
final Method method = providerClass.getDeclaredMethod("createSession");
assertTrue(method.isAnnotationPresent(RequestMapping.class));
final RequestMapping mapping = method.getAnnotation(RequestMapping.class);
assertEquals("sessions", mapping.path());
assertEquals(0, method.getParameters().toString());
}
}
似乎还没有关于这个问题的解决方案...
这是我解决这个问题的方法:
将此注释添加到您的测试中 class:
@ImportAutoConfiguration({RibbonAutoConfiguration.class, FeignRibbonClientAutoConfiguration.class, FeignAutoConfiguration.class})
尝试一下,如果它不起作用,您可能需要在主程序配置中添加 @EnableFeignClients
注解
推荐的方法是切片应用程序配置,这意味着您需要从SpringBootApplication.[=14中删除@EnableFeignClients
=]
并添加专用配置class:
@Configuration
@EnableFeignClients
public class CloudConfiguration {
}
这是必需的,因为所有切片注释(如@WebMvcTest)都包含来自 SpringBootApplication 的默认配置。
参考:
唯一你必须做的:
- 添加到你的构建文件假依赖,例如,gradle
编译'org.springframework.cloud:spring-cloud-starter-feign'
- 将@FeignClient 添加到您的界面
- 将@EnableFeignClients 添加到任何配置或添加到带有注释@SpringBootApplication
的class
我通过在 class
上添加 @EnableAutoConfiguration
注释解决了这个问题
我也遇到了这个问题,我遵循了 MariuszS 的回答。但是必须弄清楚一些事情所以放在这里......我们需要告诉@SpringBootTest 从哪里获取配置所以它会像这样
@SpringBootTest
@ContextConfiguration(classes = { YourClientConfig.class })
我正在编写的微服务需要与我们平台中的其他微服务进行通信。在那次尝试中,我们的理想解决方案是 Spring Cloud Netflix Feign,实施 @FeignClient
.
但是,当我尝试 @Autowired ReviewProvider
:
异常(原因)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.netflix.feign.FeignContext' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:155)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
ReviewProvider.java
@FeignClient("http://metadata-reviews")
public interface ReviewProvider {
@RequestMapping(path = "sessions", method = POST)
ReviewSessionDTO createSession();
}
ReviewProvider.java
@RunWith(SpringRunner.class)
@ActiveProfiles(INTEGRATION)
@ContextConfiguration(classes = AppEntry.class)
@AutoConfigureTestDatabase(replace = Replace.NONE)
@DataJpaTest
public class ReviewProviderTest {
@Autowired
private ReviewProvider provider;
private Class<? extends ReviewProvider> providerClass;
@Before
public void setup() {
providerClass = provider.getClass();
}
@Test
public void classAnnotations() {
assertTrue(providerClass.isAnnotationPresent(FeignClient.class));
assertEquals("http://metadata-reviews", providerClass.getAnnotation(FeignClient.class).value());
}
@Test
public void createSession() throws Exception {
final Method method = providerClass.getDeclaredMethod("createSession");
assertTrue(method.isAnnotationPresent(RequestMapping.class));
final RequestMapping mapping = method.getAnnotation(RequestMapping.class);
assertEquals("sessions", mapping.path());
assertEquals(0, method.getParameters().toString());
}
}
似乎还没有关于这个问题的解决方案...
这是我解决这个问题的方法:
将此注释添加到您的测试中 class:
@ImportAutoConfiguration({RibbonAutoConfiguration.class, FeignRibbonClientAutoConfiguration.class, FeignAutoConfiguration.class})
尝试一下,如果它不起作用,您可能需要在主程序配置中添加 @EnableFeignClients
注解
推荐的方法是切片应用程序配置,这意味着您需要从SpringBootApplication.[=14中删除@EnableFeignClients
=]
并添加专用配置class:
@Configuration
@EnableFeignClients
public class CloudConfiguration {
}
这是必需的,因为所有切片注释(如@WebMvcTest)都包含来自 SpringBootApplication 的默认配置。
参考:
唯一你必须做的:
- 添加到你的构建文件假依赖,例如,gradle
编译'org.springframework.cloud:spring-cloud-starter-feign'
- 将@FeignClient 添加到您的界面
- 将@EnableFeignClients 添加到任何配置或添加到带有注释@SpringBootApplication 的class
我通过在 class
上添加@EnableAutoConfiguration
注释解决了这个问题
我也遇到了这个问题,我遵循了 MariuszS 的回答。但是必须弄清楚一些事情所以放在这里......我们需要告诉@SpringBootTest 从哪里获取配置所以它会像这样
@SpringBootTest
@ContextConfiguration(classes = { YourClientConfig.class })