Singleton property on springboot

Viewed 3974

Can I create one singleton property in spring boot?

When I use this:

public class MessengerPlatformCallbackHandler {

    @Scope(value = "singleton")
    private Map<String, Object> conversationID = new HashMap<>();

I got the erro: @Scope not applicable to field

tks

2 Answers

You need to create it this way.

@Configuration
public class ConversationIDConfig {
    @Bean
    public Map<String, Object> conversationId(){
        return new HashMap<>();
    }
}

And later you can inject it where ever you want as below.

public class MessengerPlatformCallbackHandler {

    @Autowired
    private Map<String, Object> conversationId;

}
Related