@FeignClient注入找不到异常

前言

Springcloud中的服务间调用是通过Feign进行调用的,在调用方服务中,我们需要定义一些带有@FeignClient注解的接口类。并且在启动类上加上@EnableFeignClients注解。程序启动的时候,会检查是否有@EnableFeignClients注解,如果有该注解,则开启包扫描,扫描带有@FeignClient注解的接口。这里结合之前遇到的一个问题来和大家一起学习下@EnableFeignClients启动过程。

问题描述

之前搭建一个简单demo的时候,启动之后总是报错

Field client1Feign in com.aiqinhai.client2.controller.Testrequired a bean of type
 'com.aiqinhai.client2.feignclient.Client1Feign' that could not be found.
Action:
Consider defining a bean of type 'com.aiqinhai.client2.feignclient.Client1Feign' in your configuration.
Process finished with exit code 1

排查了一顿之后,发现启动上的@EnableFeignClients注解,没有指定basePackages包扫描路径。而且Client1Feign接口不是和启动类在同一目录下,所以启动的时候就会报上述错误。后来在@EnableFeignClients指定了扫描包路径就解决了。

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {"com.aiqinhai.client2.feignclient"})
public class Client2Application {
    public static void main(String[] args) {
        SpringApplication.run(Client2Application.class, args);
    }
}

问题确实解决了,但是我们还是需要了解服务启动的时候,@EnableFeignClients注解干了哪些事。

刨根@EnableFeignClients

想了解清楚这个注解的作用,最好的方式就是看看注解的源码。点进去之后,可以看到

/**
 * 扫描注解@FeignClient标注的接口
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import(FeignClientsRegistrar.class)
public @interface EnableFeignClients {
	//同basePackages
	String[] value() default {};
        //扫描的package.
	String[] basePackages() default {};
	//feigin client全局配置,默认配置在FeignClientsConfiguration类中
	Class[] defaultConfiguration() default {};
	//@FeignClient注解的接口,如果指定了该属性,则关闭扫描。
	Class[] clients() default {};
}

可以看到,注解中import了FeignClientsRegistrar类,我们进去看看,可以看到包扫描的逻辑就是在FeignClientsRegistrar中实现的,会扫描所有的.class文件,过滤出@FeignClient标注的接口,然后通过BeanDefinitionBuilder生成FeignClientFactoryBean对象,注入到IOC容器中。具体代码如下

class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
		ResourceLoaderAware, EnvironmentAware {
	//包扫描方法入口
	@Override
	public void registerBeanDefinitions(AnnotationMetadata metadata,
			BeanDefinitionRegistry registry) {
		//注册默认配置bean到ioc
		registerDefaultConfiguration(metadata, registry);
		//注册@FeignClients标注的接口bean,为其生成动态代理
		registerFeignClients(metadata, registry);
	}
	private void registerDefaultConfiguration(AnnotationMetadata metadata,
			BeanDefinitionRegistry registry) {
		//获取@EnableFeignClients注解属性
		Map defaultAttrs = metadata
				.getAnnotationAttributes(EnableFeignClients.class.getName(), true);
		if (defaultAttrs != null && defaultAttrs.containsKey("defaultConfiguration")) {
			String name;
			if (metadata.hasEnclosingClass()) {
				name = "default." + metadata.getEnclosingClassName();
			}
			else {
				name = "default." + metadata.getClassName();
			}
			//注册@EnableFeignClients defaultConfiguration 类中定义的bean到ioc
			registerClientConfiguration(registry, name,
					defaultAttrs.get("defaultConfiguration"));
		}
	}
	public void registerFeignClients(AnnotationMetadata metadata,
			BeanDefinitionRegistry registry) {
		//spring 扫描工具类
		ClassPathScanningCandidateComponentProvider scanner = getScanner();
		scanner.setResourceLoader(this.resourceLoader);
        //待扫描的包
		Set basePackages;
        //@EnableFeignClients注解属性
		Map attrs = metadata
				.getAnnotationAttributes(EnableFeignClients.class.getName());
		//@FeignClient注解过滤器,只扫描@FeignClient注解标注的接口
		AnnotationTypeFilter annotationTypeFilter = new AnnotationTypeFilter(
				FeignClient.class);
		//解析@EnableFeignClient 属性clients
		final Class[] clients = attrs == null ? null
				: (Class[]) attrs.get("clients");
		//如果@EnableFeignClient没有指定clients,则开启包扫描,否则关闭扫描,使用clients
		if (clients == null || clients.length == 0) {
			scanner.addIncludeFilter(annotationTypeFilter);
			basePackages = getBasePackages(metadata);
		}
		else {
			final Set clientClasses = new HashSet<>();
			basePackages = new HashSet<>();
			for (Class clazz : clients) {
				basePackages.add(ClassUtils.getPackageName(clazz));
				clientClasses.add(clazz.getCanonicalName());
			}
			AbstractClassTestingTypeFilter filter = new AbstractClassTestingTypeFilter() {
				@Override
				protected boolean match(ClassMetadata metadata) {
					String cleaned = metadata.getClassName().replaceAll("\\$", ".");
					return clientClasses.contains(cleaned);
				}
			};
			scanner.addIncludeFilter(
					new AllTypeFilter(Arrays.asList(filter, annotationTypeFilter)));
		}
		for (String basePackage : basePackages) {
		    //从类路径下的所有.class文件中扫描@FeignClient注解的接口
			Set candidateComponents = scanner
					.findCandidateComponents(basePackage);
			for (BeanDefinition candidateComponent : candidateComponents) {
				if (candidateComponent instanceof AnnotatedBeanDefinition) {
					// verify annotated class is an interface
					AnnotatedBeanDefinition beanDefinition = (AnnotatedBeanDefinition) candidateComponent;
					//获取注解
					AnnotationMetadata annotationMetadata = beanDefinition.getMetadata();
					//@FeignClient注解必须应用在接口上,否则抛出异常。
					Assert.isTrue(annotationMetadata.isInterface(),
							"@FeignClient can only be specified on an interface");
                        //获取FeignClient注解属性
					Map attributes = annotationMetadata
							.getAnnotationAttributes(
									FeignClient.class.getCanonicalName());
                        //服务名称
					String name = getClientName(attributes);
					//注册configuration中指定的bean
					registerClientConfiguration(registry, name,
							attributes.get("configuration"));
					//生成FeignClient bean,并注册到ioc
					registerFeignClient(registry, annotationMetadata, attributes);
				}
			}
		}
	}

    //生成bean,注入到IOC容器
	private void registerFeignClient(BeanDefinitionRegistry registry,
			AnnotationMetadata annotationMetadata, Map attributes) {
		// feignclient类名称
		String className = annotationMetadata.getClassName();
		BeanDefinitionBuilder definition = BeanDefinitionBuilder
				.genericBeanDefinition(FeignClientFactoryBean.class);
		validate(attributes);
		definition.addPropertyValue("url", getUrl(attributes));
		definition.addPropertyValue("path", getPath(attributes));
		String name = getName(attributes);
		definition.addPropertyValue("name", name);
		definition.addPropertyValue("type", className);
		definition.addPropertyValue("decode404", attributes.get("decode404"));
		definition.addPropertyValue("fallback", attributes.get("fallback"));
		definition.addPropertyValue("fallbackFactory", attributes.get("fallbackFactory"));
		definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);

		String alias = name + "FeignClient";
		AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();

		boolean primary = (Boolean)attributes.get("primary"); // has a default, won't be null
		beanDefinition.setPrimary(primary);
		String qualifier = getQualifier(attributes);
		if (StringUtils.hasText(qualifier)) {
			alias = qualifier;
		}
		BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className,
				new String[] { alias });
		//注册到ioc
		BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
	}
}

从上面的代码中,可以看到@EnableFeignClients的包扫描逻辑。

你可能感兴趣的:(微服务)