Returning Singleton beans depending on input in Spring

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Returning Singleton beans depending on input in Spring



Let's say I have the following:


@Component
@NoArgsConstructor
public class ToolFactory {

public Tool getTool(String type) {
return StaticToolProvider.getTool(type);
}
}



If the tools can be screwdriver, hammer, or wrench, I want Spring to create Singleton beans for each, and return them when getTool() is called. I believe @Provides @Singleton would do this in Guice, but how could I do it here?


getTool()


@Provides @Singleton





give details. what you want to do
– sajib
6 mins ago





I will only ever return a screwdriver, hammer, or wrench, but I don't want new prototype beans returned for every call. I only want 3 beans to only ever be created, but I want them created as getTool() is called, or the existing Singleton returned if it exists.
– radgokart
4 mins ago




getTool()





You have screwdriver, hammer bean which implements Tool?
– sajib
1 min ago





add more code and ask what you have and what you want
– sajib
1 min ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived