Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @RefreshScope可以';我无法与@Bean相处_Java_Spring_Javabeans_Spring Cloud - Fatal编程技术网

Java @RefreshScope可以';我无法与@Bean相处

Java @RefreshScope可以';我无法与@Bean相处,java,spring,javabeans,spring-cloud,Java,Spring,Javabeans,Spring Cloud,我有一个需要从配置服务器刷新配置的控制器,所以我在它上面添加了@RefreshScope。同时,该控制器需要调用后端API,以便我定义RESTTemplatebean。但一旦启动此应用程序,就会出现异常。有谁能告诉我这两个注释为什么要引用 Error: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'scopedTarget.frontEndAppli

我有一个需要从配置服务器刷新配置的控制器,所以我在它上面添加了@RefreshScope。同时,该控制器需要调用后端API,以便我定义RESTTemplatebean。但一旦启动此应用程序,就会出现异常。有谁能告诉我这两个注释为什么要引用

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?

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


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

有文件证明,刷新范围不适用于
@Configuration
类,spring boot应用程序需要这些类的答案。代码很奇怪,因为我只是在做一些研究。
@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;

}