whereeaglesdare
@whereeaglesdare

Почему возникает сircular reference для scope.SINGLETON?

Добрый вечер
В моем spring-boot приложении возникает сircular reference, и я не могу понять почему.
В точке входа в приложение у меня следующие методы:
@Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    public DescriptorSingleton getDescriptorSingleton() {
        return descriptorMock.descriptorSingleton();
    }

    @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    public StateSingleton getStateSingleton() {
        return stateConfiguration.getStateClasses();
    }


вот часть файла StateConfiguration:
@Component
public class StateConfiguration {
    private static final String PROPS_PATH = "classpath:my-properties.properties";

    private static final Logger log = LoggerFactory.getLogger(StateConfiguration.class);

    @Autowired
    ResourceLoader resourceLoader;


    public Resource loadResource() {
        return resourceLoader.getResource(PROPS_PATH);
    }

    public Properties getProperties(File file) {
        InputStream input = null;
        try {
            input = new FileInputStream(file);
        } catch (IOException exception) {
            throw new  RuntimeException("Fatal IO exception");
        }
        try {
            Properties prop = new Properties();
            prop.load(input);
            return prop;
        } catch (IOException exception) {
            throw new RuntimeException("Cannot get properties from InputStream");
        }
    }

    @PostConstruct
    public StateSingleton getStateClasses() {
        log.info("Getting state k/v pairs");

        File resource = null;
        try {
            resource = loadResource().getFile();
        } catch (IOException exception) {
            throw new RuntimeException("Cannot file properties file");
        }

        Properties properties = getProperties(resource);
        Map<String, String> params = properties.entrySet().stream()
                .collect(Collectors.toMap(entry -> entry.getKey().toString(),
                        entry -> entry.getValue().toString()));
        return StateSingleton.create(params);
    }
}


ну и DescriptorMock.java

@Component
public class DescriptorMock {

    @PostConstruct
    public DescriptorSingleton descriptorSingleton() {
        return DescriptorSingleton.create();
    }
}


При этом, если я в основном классе моего приложения буду получать DescriptorSingleton через класс StateConfiguration, то все будет работать замечательно:

@Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    public DescriptorSingleton getDescriptorSingleton() {
        return stateConfiguration.getDescriptorSingleton();
    }

    @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    public StateSingleton getStateSingleton() {
        return stateConfiguration.getStateClasses();
    }


В чем моя ошибка?
  • Вопрос задан
  • 61 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
Bell Integrator Ульяновск
До 400 000 ₽
Bell Integrator Хабаровск
До 400 000 ₽
Bell Integrator Ижевск
До 400 000 ₽
18 апр. 2024, в 21:56
2000 руб./за проект
18 апр. 2024, в 21:00
150 руб./за проект