本篇文章给大家谈谈@EnableFeignClients和@FeignClient在自动装配'FeignContext'时失败NoSuchBeanException,同时本文还将给你拓展2)集成Fei
本篇文章给大家谈谈@EnableFeignClients和@FeignClient在自动装配'FeignContext'时失败NoSuchBeanException,同时本文还将给你拓展2) 集成FeignClient、9-6 FeignClient 参数讲解、@ComponentScan、@EnableFeignClients和@MapperScan注解笔记、@EnableFeignClient 默认只扫描注解所在包的当前包及子包等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- @EnableFeignClients和@FeignClient在自动装配'FeignContext'时失败NoSuchBeanException
- 2) 集成FeignClient
- 9-6 FeignClient 参数讲解
- @ComponentScan、@EnableFeignClients和@MapperScan注解笔记
- @EnableFeignClient 默认只扫描注解所在包的当前包及子包
@EnableFeignClients和@FeignClient在自动装配'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)@DataJpaTestpublic 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()); }}
答案1
小编典典似乎还没有关于此问题的解决方案的任何信息…
这是我为解决此问题所做的工作:1.将此注释添加到测试类中:
@ImportAutoConfiguration({RibbonAutoConfiguration.class, FeignRibbonClientAutoConfiguration.class, FeignAutoConfiguration.class})
尝试一下,如果它不起作用,则可能需要在@EnableFeignClients
主程序配置上添加注释
2) 集成FeignClient
添加依赖包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
定义接口
@FeignClient(value = "user-web")
public interface UserWebServiceApi {
/**
* <p>查找用户信息</p>
*
* @param account
* @return
*/
@RequestMapping(value = "/userweb/findByAccount", produces = "application/json")
public ResultVO<UserInfo> findByAccount(@RequestParam("account") String account);
}
其中@FeignClient(value = "user-web", fallback = UserWebServiceFallback.class)的user-web是提供者的服务名spring.application.name
@RequestMapping(value = "/userweb/findByAccount", produces = "application/json") 需要对应提供者的完整路径,并且参数与返回值需要一致
启动类添加启用FeignClient注解
@EnableFeignClients(basePackages = {"com.zhoulp.web"})
最后在代码里面注入调用方法即可
@Inject
private UserWebServiceApi userInfoServiceApi;
userInfoServiceApi.findByAccount("zhangsan");
9-6 FeignClient 参数讲解
这里也把 s 去掉
我们的路径这里都是 Provider
上面使用 path 参数,这样下面就不需要写 provider 了
重启 provider 和 consumer 服务。正常运行说明我们的 Path 属性生效了。
进阶参数
FallBack 和 FallbackFactory 前提是和 Hystrix 做继承
结束
@ComponentScan、@EnableFeignClients和@MapperScan注解笔记
@ComponentScan:此注解是用来管理容器中的bean,即是管理项目中类的依赖关系, 注意此注解并不创建类的实例; 默认情况下此注解扫描本工程下的所有包, 但是在springBoot的分布式中如果需要用到别的微服务工程中的实例,那么就要写为如下的形式。注意要加上本工程。 因为当使用 basePackages时,此注解就不会使用默认的扫描路径了。
@ComponentScan(basePackages = {
"com.wisdombud.dth.boss.customer", "com.wisdombud.dth.boss.his",
"com.wisdombud.dth.boss.product.srv",
"com.wisdombud.dth.boss.product.mapper" })
@EnableFeignClients: 此注解的作用是扫描标记了@FeignClient的接口并创建实例bean,默认扫描并创建所在工程下的包。如果在springBoot的分布式中需要用到别 的微服务的工程的接口实例,那么就要写成如下的形式。注意此注解并不管理bean的依赖的关系
@EnableFeignClients(
basePackages = { "com.wisdombud.dth.boss.customer",
"com.wisdombud.dth.boss.his",
"com.wisdombud.dth.boss.product.srv"
})
@MapperScan: 此注解是扫描被此注解中声明的mapper路径下的*mapper.xml文件,读取文件配置,并根据此文件中的mapper接口路径创建bean实例。如下写法。
/***
* 功能:客户datasource 配置.<br/>
* *date:2018 年10月11日 下午2:17:31<br/>
* **
*
* @author
* joseph
* @since
* JDK 1.8
*/
@Configuration
@EnableConfigurationProperties
@MapperScan(basePackages = "com.wisdombud.dth.boss.customer.mapper", sqlSessionTemplateRef = "custSqlSessionTemplate")
public class DataSourceCustConfig {
@Bean
@ConfigurationProperties(prefix = "spring.jta.atomikos.datasource.cust")
@Primary
public AtomikosDataSourceBean custDataSource() {
return new AtomikosDataSourceBean();
}
@Bean
@Primary
public SqlSessionFactory custSqlSessionFactory(@Qualifier("custDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean
@Primary
public SqlSessionTemplate custSqlSessionTemplate(
@Qualifier("custSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@EnableFeignClient 默认只扫描注解所在包的当前包及子包
rt
关于@EnableFeignClients和@FeignClient在自动装配'FeignContext'时失败NoSuchBeanException的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于2) 集成FeignClient、9-6 FeignClient 参数讲解、@ComponentScan、@EnableFeignClients和@MapperScan注解笔记、@EnableFeignClient 默认只扫描注解所在包的当前包及子包的相关知识,请在本站寻找。
本文标签: