In a CDI Passivation Capable bean, is it possible to have non-passivation capable dependencies be re-injected rather than passivated?
Consider this code:
@SessionScoped
public class UserData implements Serializable {
@Inject
private Logger log;
private String data;
}
@ApplicationScoped
public class LoggerFactory {
@Produces
public Logger getLogger(){
...
}
}
public class Logger {
...
}
So Logger
is not Serializable
, but I really don't care. When UserData
is deserialized, is it possible to have the producer for Logger
called again somehow?
The original discussion started here:
http://www.cdi-spec.org/news/2015/07/03/CDI-2_0-EDR1-released/#comment-2119769909
Hoping the CDI Expert group comes up with a better way than @Instance
Checking the spec, you have your answer. Logger
is not serializable, so the bean of type Logger
is not passivation capable. The container doesn't provide the trick you are requesting.
The solution would be to write something like that:
@SessionScoped
public class UserData implements Serializable {
@Inject
private Instance<Logger> logInstance;
private String data;
public Logger getLog() {
return logInstance.get();
}
}
Ans use getLog()
instead of log
in your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With