CDI Injection and Serialization

I am considering using CDI injection for slf4j logger, so I created a manufacturer.

I inject it into the ApplicationScoped bean, which is serializable:

 @ApplicationScoped public final class CurrentApplicationBean implements Serializable { @Inject private transient Logger logger; } 

It should be temporary because org.slf4j.Logger is an interface that does not extend Serializable , but that means the registrar must be re-injected after deserialization.

I think CDI is not up to the challenge, what do you know?

In addition, the provider always provides a new Logger beacuse instance, it must set the log name from InjectionPoint , which means that RequestScoped beans has its own log instance, and not static for each class registrar.

Maybe journaling is not a good context for CDI injections ... what are your thoughts?

+4
source share
1 answer

but this means that the registrar must be re-injected after deserialization.

The CDI Container proxy is serialized. Upon deserialization, the proxy detects / binds the correct injection. I would not mark the injection point as a transition; as this will prevent container detection / re-injection and lead to NPE.

this means that RequestScoped beans has its own logger instance instead of static in the log log

If your manufacturer method is similar to the following

 @RequestScoped @Produces public Logger produceLog(InjectionPoint injectionPoint) { return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName()); } 

LoggerFactory.getLogger () creates one logger for each class.

Maybe journaling is not a good context for CDI injections ... what are your thoughts?

This is your choice.

+1
source

All Articles