Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to prohibit using a class as spring bean?

Is there a way (any annotation of base class) using which i can mark a class such that spring throws an error on context initialization? Basically i have a class which is not thread safe by design and i do not want anybody to use it with spring as singleton bean. I know it can be used as prototype bean but can i prohibit, it being used as spring bean altogether?

like image 977
smi Avatar asked Jan 21 '26 12:01

smi


1 Answers

Make its post-initialisation method throw.

Either implement InitializingBean

public final void afterPropertiesSet() {
    throw new UnsupportedOperationException();
}

Or use a JEE annotation

@PostConstruct
public final void forbidDependencyInjection() {
    throw new UnsupportedOperationException();
}
like image 156
OrangeDog Avatar answered Jan 24 '26 02:01

OrangeDog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!