[cdi-dev] [JBoss JIRA] (CDI-228) Clarify that _all_ @Dependent beans created for a containers method invocation will get destroyed after the method exits

Mark Struberg (JIRA) jira-events at lists.jboss.org
Thu May 3 09:22:18 EDT 2012


    [ https://issues.jboss.org/browse/CDI-228?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12690038#comment-12690038 ] 

Mark Struberg commented on CDI-228:
-----------------------------------

Thanks for the Observer catch, Martin! I did knew that we have it somewhere, but didn't find it quickly ;)

> I don't think we should declare this for bean constructors and initializer method parameters 
> - such contextual instances logically depend on the declaring bean.

The problem here is that the CDI-1.0 spec was overly strict with the Serialization checks. We generally didn't allow non-serializable @Dependent beans getting injected into a passiviting Scope beans. But thats bollocks as well.

There are 2 important use cases

A.) the @Inject method param gets stored inside the class
public class X {
  Y y;

  @Inject 
  public initY(Y myY) {
     myY.doSomeInit();
     this.y = myY;
  }
}

B.) the @Inject method param gets NOT stored inside the class
public class X {
  Z z;

  @Inject 
  public initY(Y myY) {
     z = myY.createZ();
  }
}



                
> Clarify that _all_ @Dependent beans created for a containers method invocation will get destroyed after the method exits
> ------------------------------------------------------------------------------------------------------------------------
>
>                 Key: CDI-228
>                 URL: https://issues.jboss.org/browse/CDI-228
>             Project: CDI Specification Issues
>          Issue Type: Clarification
>          Components: Contexts
>    Affects Versions: 1.1.EDR1
>            Reporter: Mark Struberg
>
> This clarification is intended for all methods which gets invoked by the CDI container and create a new @Dependent contextual instance especially for this invocation. This can happen in @Observes, @Produces, @Disposal and @Inject methods as well as in @Inject contructors. Basically any @Dependent method-parameter InjectionPoint.
> Despite it's atm not specified whether this @Dependent instance will get stored, most containers store it in the CreationalContext of the bean containing the invoked method. This behaviour can lead to mem leaks and non-serializibility issues.
> TASK: Define that any @Dependent contextual instance will get properly destroyed after such method invocations. 
> There are 2 things we need to think about:
> 1.) any @PreDestroy method of those beans will get invoked after the method invocation, even if the @Dependent instance will stored away in a member field and still being used later. This will not make any problems in most cases. We just need to make people aware that this will happen.
> 2.) As any Decorator or Interceptor is also an @Dependent instance on our 'temporary' created @Dependent method parameter, those Interceptors and Decorators will _not_ be available after the method invocation. Storing away this bean and re-using it later will probably cause an Exception.
> I still think this is a small problem compared to creating tons of mem leaks. There are quite a few workarounds possible: 
> *) Instead of @Inject methods you can use @Inject field + @PostConstruct to initialize it.
> *) We might add an additional annotation which denotes either @Transactional or au contraire: @Keep for the method-param InjectionPoint

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.jboss.org/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the cdi-dev mailing list