我有一个java类,如下所示:
@RequiredArgsConstructor
public class Interceptor implements RequestInterceptor {
@Value("${spring.application.name}")
private final String applicationName;
}
这个类由一个客户端使用:
@FeignClient(
name = "interceptor",
url = "${interceptor.url}",
configuration = Interceptor.class
)
public interface InterceptorApiClient {
}
问题是,当我尝试运行项目时,我得到了这个错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1801)
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1357)
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311)
at app//org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
at app//org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
... 181 more
我放了@Component注解,给予了@PropertySource({“classpath:application.yaml”}),但没有任何变化。
注意:applicationName在application.yaml文件中,如下所示:
spring:
application:
name: interceptor-project
知道吗?
1条答案
按热度按时间6fe3ivhb1#
使用@Value关键字注解“applicationName”字段的setter方法,而不是字段本身。通过这样做,Spring将能够注入属性的值,而不需要寻找类型为“String”的bean
希望这能回答你问题