[cdi-dev] [JBoss JIRA] (CDI-576) Provide an API to say a bean depends on another instance

Romain Manni-Bucau (JIRA) issues at jboss.org
Fri Dec 18 17:13:00 EST 2015


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

Romain Manni-Bucau commented on CDI-576:
----------------------------------------

[~struberg] this is what I said (on the broken side) but it still needs a solution. "just touch that stuff" DOESNT solve anything since you have to know an API you can touch for proxies which is not obvious + as explained in the description you can not be able to do it if it is in several sublayers (it is more common that we think and why @DependsOn wouldnt even work in 50% of cases).

See this very simplified case (jira pseudo-coding so please forget typo etc):

{code}
class InstanceManager {
  @Produces @ApplicationScoped MyInstance produce() { ... }
  void release(@Disposes MyInstance instance) {...}
}
{code}



{code}
class InstanceUsage1 {
  @Inject
  MyInstance instance;

  void start(@Observes @Initialized(AppScoped.class) Object start) {
    instance.doWork();
  }
}
{code}


{code}
@Startup
@Singleton // or any other start thing
class InstanceUsage2 {
  @Inject
  MyWorker worker;

  @PostConstruct
  void init() {
    worker.startJobs();
  }
}
{code}



{code}
class MyWorker {
  @Inject
  MyInstance instance;

  void doWork() {
    instance.doSomethingElse();
  }
}
{code}

This use case is : not orderable events (in previous samples the startup event) we need to be able to enforce dependencies to ensure the InstanceManager is started as expected at the very beginning.

You can touch MyInstance in InstanceUsage1 but likely not in InstanceUsage2 where it is also needed.

The case of startup is obvious and you can surely argue it is not 100% CDI - but it is real - but it is the same if you have a resource while would be hit later in the request/session/... and you need to validate it exists before starting the flow. Of course you can "touch" it but then if you mock it or specialize it your touching code can just be wrong and corrupt your application. Since we have the metadata in extensions I don't see any issue to ask for an eager instantiation there. Even adding constraints for such a feature (like scopes supporting it etc) is perfectly valid and fine but not having it makes some cases very hard to support IMHO.


> Provide an API to say a bean depends on another instance
> --------------------------------------------------------
>
>                 Key: CDI-576
>                 URL: https://issues.jboss.org/browse/CDI-576
>             Project: CDI Specification Issues
>          Issue Type: Epic
>            Reporter: Romain Manni-Bucau
>
> Sometimes you want to initialize a bean before another bean. This is typically the case of @DependsOn with EJB but it can be more vicious and through several layers.
> If A depends on B which depends on C then we can desire to initialize C before A but if A, B and C are normal scoped then it will not happen before C is touched before touching A.
> We can't guarantee we can find the dependency and it is not desirable to hardcode it sometimes (test mock shouldnt go in "main" code typically). That's why having an event (guess it will belong to extensions) allowing to require this kind of eager initialization can be nice.
> we would get something like
> {code}
> event.dependencyOn(bean1, bean2);
> {code}
> and it would be fired before after validation event.
> The runtime behavior will be to create bean2 before bean1 (I have no issue reversing params/renaming the method). Only trick will be for not normal scoped beans where we can need to reuse the same instance but it sounds feasible in term of implementation since we'll be in the "create" stack anyway.



--
This message was sent by Atlassian JIRA
(v6.4.11#64026)


More information about the cdi-dev mailing list