Answer the question
In order to leave comments, you need to log in
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();
}
@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);
}
}
@Component
public class DescriptorMock {
@PostConstruct
public DescriptorSingleton descriptorSingleton() {
return DescriptorSingleton.create();
}
}
@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public DescriptorSingleton getDescriptorSingleton() {
return stateConfiguration.getDescriptorSingleton();
}
@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public StateSingleton getStateSingleton() {
return stateConfiguration.getStateClasses();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question