E
E
Evgeniy Rybalko2020-07-01 23:19:18
Java
Evgeniy Rybalko, 2020-07-01 23:19:18

Why is there a circular reference for scope.SINGLETON?

Good evening
I have a circular reference in my spring-boot application and I can't figure out why.
At the application entry point, I have the following methods:

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

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


here is part of the StateConfiguration file:
@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);
    }
}


well, DescriptorMock.java

@Component
public class DescriptorMock {

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


At the same time, if I receive DescriptorSingleton through the StateConfiguration class in the main class of my application, then everything will work fine:

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

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


What's my mistake?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question