I\'m trying to inject a logger object in a class that is implemented following a singleton approach.
The code almost looks like this:
Logger
class:<
This will not work, because injection, as already mentioned, will be performed after the constructor is called.
Methods annotated with @PostConstruct
are called after injection has been finished and before the object itself will be supplied somewhere else.
However, injection only works if the instance of your class is provided by injection itself. This is due to the injection depending on proxying.
Therefore you will need to inject your MySingleton wherever you need it. To be sure it is a singleton, annotate it @Singleton
and the container will work that out for you.
Addiotnally beware, that singleton in terms of CDI spec does not mean only one instantiation, but rather only one initialiation of @PostConstruct
.