You can also use custom scopes to change the returned instance depending on your logic.
This may be a little cleaner than having too much logic in the providers.


On 10/19/2014 06:55 PM, Nate Bauernfeind wrote:
Do you ever run two tasks simultaneously? If not, perhaps you can create a special provider that you can swap out the Caller where you're currently considering creating a new injector.

import com.google.inject.Provider;
import java.util.concurrent.atomic.AtomicReference;
public class CallerProvider implements Provider<Caller> {
private AtomicReference<Caller> caller;
public void set(Caller currentCaller) {
caller.set(currentCaller);
}
@Override
public Caller get() {
return caller.get();
}
}

Inject Provider<Caller> anywhere you need to know who the caller is (when logging, etc) and call .get() to get the current caller. Consider using a Provider<Optional<Caller>> or at least be careful of null pointer exceptions when Caller is not set.

Does this help you get to a solution? Are there other things you were hoping to replace by creating a new injector that doesn't fit into this pattern?


On Sun, Oct 19, 2014 at 11:54 AM, Kevin Burton <burtona...@gmail.com <mailto:burtona...@gmail.com>> wrote:

    I'm aware that it's probably that I might need to refactor some
    things ...

    However, I'm not sure the ideal way to have certain bindings,
    without replacing them.

    I have one binding, Caller, which is literally just a string
    explaining what root service called an object.

    This way I can trace things like how an HTTP request originated.

    By default, it's unknown... because any library should be able to
    perform an HTTP request if necessary.

    However, I need to swap in a new one as I'm starting up certain tasks.

    So if I have a Foo and a Bar task each should have its own Caller.

    And yes, you might be right...  I may need to rethink some of my
    bindings... some of these aren't completely obvious though.
-- You received this message because you are subscribed to the Google
    Groups "google-guice" group.
    To unsubscribe from this group and stop receiving emails from it,
    send an email to google-guice+unsubscr...@googlegroups.com
    <mailto:google-guice+unsubscr...@googlegroups.com>.
    To post to this group, send email to google-guice@googlegroups.com
    <mailto:google-guice@googlegroups.com>.
    Visit this group at http://groups.google.com/group/google-guice.
    To view this discussion on the web visit
    
https://groups.google.com/d/msgid/google-guice/41e3b741-ec9b-49a2-bebe-b8881bd48aa7%40googlegroups.com
    
<https://groups.google.com/d/msgid/google-guice/41e3b741-ec9b-49a2-bebe-b8881bd48aa7%40googlegroups.com?utm_medium=email&utm_source=footer>.


    For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+unsubscr...@googlegroups.com <mailto:google-guice+unsubscr...@googlegroups.com>. To post to this group, send email to google-guice@googlegroups.com <mailto:google-guice@googlegroups.com>.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/CAHNex9-2Wn2_xdsgW%3D8wurkw16p2ZkU2cQdm770DLs3KA-cEgw%40mail.gmail.com <https://groups.google.com/d/msgid/google-guice/CAHNex9-2Wn2_xdsgW%3D8wurkw16p2ZkU2cQdm770DLs3KA-cEgw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-guice+unsubscr...@googlegroups.com.
To post to this group, send email to google-guice@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/544407CA.2090906%40gmx.ch.
For more options, visit https://groups.google.com/d/optout.

Reply via email to