java - Spring Boot 应用程序给出错误循环引用为什么?

标签 java spring-boot

我有这个配置类:-

@Configuration
public class AppConfig {

    @Autowired
    private Environment env;
    private List<Weather> weathers = new ArrayList<>();

    @Bean
    public RestTemplate restTemplate() {
        var factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(3000);
        factory.setReadTimeout(3000);
        return new RestTemplate(factory);
    }



    @PostConstruct
    public void startMonitoring() {
        String url = env.getProperty("openWeatherUrl");
        String apiKey = env.getProperty("openWeatherApiKey");

        try (InputStream resource = app.class.getResourceAsStream("/config.csv")) {
            CSVReader reader = new CSVReader(new InputStreamReader(resource));
            String[] lineInArray;
            while ((lineInArray = reader.readNext()) != null) {
                Weather weather = new Weather();
                if (!lineInArray[0].contains("city")) {
                    weather.setCity(lineInArray[0]);
                } else {
                    continue;
                }

                if (!lineInArray[1].contains("temperaturesLimit")) {
                    weather.setTemperaturesLimit(Float.parseFloat(lineInArray[2]));
                }
                if (!lineInArray[2].contains("temperaturesLimit")) {
                    weather.setFrequency(Integer.parseInt(lineInArray[2]));
                }

                URI uri = new URI(url + "?q=" + weather.getCity() + "&units=metric&APPID=" + apiKey);
                weather.setUri(uri);
                weathers.add(weather);
            }
        } catch (IOException | CsvValidationException | URISyntaxException e) {
            System.out.println(e.getMessage());
        }
        fetchWeatherData();
    }

    void fetchWeatherData(){
        RestTemplate restTemplate = restTemplate();
        weathers.forEach(weather -> {
            var res = restTemplate.getForEntity(weather.getUri(), Weather.class);
        });
    }
}

我得到了错误:-

application context中的一些bean的依赖关系形成一个循环:

行动:

不鼓励依赖循环引用,默认情况下禁止使用循环引用。更新您的应用程序以删除 bean 之间的依赖循环。作为最后的手段,可以通过将 spring.main.allow-circular-references 设置为 true 来自动打破循环。

为什么是圆形的?我该如何解决?

最佳答案

很简单 AppConfig 中的 @PostConstruct 依赖于 RestTemplate bean 但是这个 @BeanAppConfig.

这是由于 @Configuration 类的工作方式,它们被代理并且调用 public RestTemplate restTemplate() 将调用代理方法。

如果 RestTemplate 未在其他位置使用,一个简单的修复方法是从 RestTemplate 方法中删除 @Bean 注释。

关于java - Spring Boot 应用程序给出错误循环引用为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70198886/

相关文章:

java - dbf 表插入时出现语法错误

Java ConnectionPool 连接未关闭,卡在 'sleep'

java - setTemplateMode ("LEGACYHTML5") 不适用于 thymeleaf-spring-4

java - 如何查看远程 pc 文件夹中的 java 更改?

java - 如何摆脱类型转换?

css - Spring Boot 不加载 css 文件 : Request method 'GET' not supported

java - @Transactional 上的 MySQL 套接字超时行为

Java Spring Sleuth Zipkin - X-Span-Export 未显示

java - 如何在Spring Boot Rest模板中使用.pfx证书和密码调用安全的rest api?

java - Springboot : How to remove an annotation on a subclass property?