You can do this βJava wayβ with AtomicBoolean :
private static final created = new AtomicBoolean(); @PostConstruct public void ensureSingleInstance() { if(created.getAndSet(true)) { throw new IllegalStateException("Trying to create second instance"); } }
But do you really need such a statement? Beans default scope="singleton" ...
source share