没有类型为'java.lang.String'的合格Bean可用:预期至少有1个符合autowire候选条件的bean,依赖关系注解:{}

bvjveswy  于 2023-05-05  发布在  Java
关注(0)|答案(1)|浏览(177)

我有一个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

知道吗?

6fe3ivhb

6fe3ivhb1#

private String applicationName;

@Value("${spring.application.name}")
public void setApplicationName(String applicationName) {
    this.applicationName = applicationName;
}

使用@Value关键字注解“applicationName”字段的setter方法,而不是字段本身。通过这样做,Spring将能够注入属性的值,而不需要寻找类型为“String”的bean
希望这能回答你问题

相关问题