我有一个需要从配置服务器刷新配置的控制器,所以我在上面添加了@ refresh so。同时,这个控制器需要调用一个后端API,所以我定义了restTemplate Bean。但是一旦我启动了这个应用程序,异常就发生了。谁能告诉我为什么这两个注解会成为循环引用?
Error: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'scopedTarget.frontEndApplication': 
Unsatisfied dependency expressed through field 'restTemplate'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'restTemplate': Requested bean is currently in creation: Is there an unresolvable circular reference?@SpringBootApplication
@RestController
@EnableDiscoveryClient
@RefreshScope
public class FrontEndApplication {
    @Value("${msg:Hello default}")
    private String message;
    public static void main(String[] args) {
        SpringApplication.run(FrontEndApplication.class, args);
    }
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
    @Autowired
    RestTemplate restTemplate;
}发布于 2020-02-20 00:35:43
首先,不要把@RefreshScope放在控制器上。通常,您会希望在存储状态的类中执行此操作。如果是配置属性,最好在POJO上使用@ConfigurationProperty注释并调用@EnableConfigurationProperties。
而且你的主类做了所有的事情,你能不能把它分成不同的类,然后再试一次?让你的主类同时是控制器、存储库和服务并不是一个好主意。
https://stackoverflow.com/questions/60304849
复制相似问题