首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >@RefreshScope无法与@Bean相处

@RefreshScope无法与@Bean相处
EN

Stack Overflow用户
提问于 2020-02-20 00:22:06
回答 1查看 333关注 0票数 0

我有一个需要从配置服务器刷新配置的控制器,所以我在上面添加了@ refresh so。同时,这个控制器需要调用一个后端API,所以我定义了restTemplate Bean。但是一旦我启动了这个应用程序,异常就发生了。谁能告诉我为什么这两个注解会成为循环引用?

代码语言:javascript
运行
复制
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?
代码语言:javascript
运行
复制
@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;

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-20 00:35:43

首先,不要把@RefreshScope放在控制器上。通常,您会希望在存储状态的类中执行此操作。如果是配置属性,最好在POJO上使用@ConfigurationProperty注释并调用@EnableConfigurationProperties

而且你的主类做了所有的事情,你能不能把它分成不同的类,然后再试一次?让你的主类同时是控制器、存储库和服务并不是一个好主意。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60304849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档