Home > Software engineering >  Spring boot app gives error circular references why?
Spring boot app gives error circular references why?

Time:12-03

I have this Configuration class:-

@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);
        });
    }
}

I got error:-

The dependencies of some of the beans in the application context form a cycle:

Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

Why is it circular? How do I fix it?

CodePudding user response:

It's quite simple @PostConstruct in AppConfig relies on the RestTemplate bean but this @Bean is part of AppConfig.

This is due to how @Configuration classes work, they are proxied and a call to public RestTemplate restTemplate() would call the proxied method.

A simple fix would be removing the @Bean annotation from your RestTemplate method, if the RestTemplate is not used in other locations.

  • Related