Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a CDI Passivation Capable bean, is it possible to have non-passivation capable dependencies be re-injected rather than passivated?

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?

EDIT

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

like image 531
Jonathan S. Fisher Avatar asked Nov 01 '22 00:11

Jonathan S. Fisher


1 Answers

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.

like image 144
Antoine Sabot-Durand Avatar answered Nov 15 '22 06:11

Antoine Sabot-Durand