Spreading an area with Guice

I have a class from which instances must be instantiated (and destroyed) for each request (dbSession in my example) and some other classes that use instances of the request object. Like this:

webservice --> repository --> dao --> dbSession

      

(-> means use)

But then I have a similar use case, but the dbsession should not bind to the request. Like this:

webservice --> otherService --> repository --> dao --> dbSession

      

The dbSession class is currently bound to the request. For another use case, the db session must not be associated with the request. I could use qualifiers to do this, but then I need to duplicate the set of (required) provider methods for both scopes.

So I am looking for another solution. Can you distribute areas? I mean that dependencies are injected by scope objects of the injection target?

@request webservice --> @myScope otherService --> @myScope repository --> @myScope dao --> @myScope dbSession

      

In the example, all dependencies of otherService must be in the same scope as otherService (calling otherService is asynchronous).

Is this possible with Guice? And just out of curiosity: is this possible with CDI?

+3


source to share


1 answer


Am I getting it right?

First case:

@Request webservice --> @Singleton repository --> @Singleton dao --> @Request dbSession

      

What are you looking for in the second case:



@request webservice --> @myScope/@Singleton otherService --> @myScope/@Singleton repository --> @myScope/@Singleton dao --> @myScope/@noscope_session_per_query dbSession

      

I would suggest you to encapsulate your repository in PrivateModule

, and expose

just repository

, and otherService

interface. Then link in both modules dbSession

(does that mean what is EntityManager

assumed?) Provider within different limits. Remember to use Provider<T>

instead of direct input when you start mixing areas.

The second option is to use the annotation to bind the dbSession / EntityManager providers and insert it with the appropriate annotation.

0


source







All Articles