Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 894299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:21:41+00:00 2026-05-15T14:21:41+00:00

I am hoping to redefine or update some bindings within a Module which is

  • 0

I am hoping to redefine or update some bindings within a Module which is in turn used by an Injector. I realise that Modules are immutable and once a binding is sucked and injected its definition cannot change for all practical purposes.

I would like to add/change/remove bindings while keeping singletons already living in an Injector. Changing is a simple case as one can achieve this effecct by using a Provider. However dynamically adding or removing a Key (binding definition) is a bit more tricky.

Any thoughts if this should or can be practically done.

I am thinking the best way is to recreate an injector each time an add/remove binding occurs copying definitions from the original to the new one, Any existing singletons would be redefined by toInstance rather than to implementation type etc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:21:41+00:00Added an answer on May 15, 2026 at 2:21 pm

    Child injectors can be used to accomplish this, but it requires some setup. Child injectors will prevent long-lived bindings from depending on short-lived ones. Here’s an example:

    class ForeverModule extends AbstractModule {
      ...
    }
    
    class TemporaryModule extends AbstractModule {
      ...
    }
    
    class Main {
        public static void main(String... args) {
            Injector foreverInjector = Guice.createInjector(new ForeverModule());
    
            Injector injector = foreverInjector.createChildInjector(
                    new TemporaryModule());
            /*
             * Do stuff with the injector as you would normally. When you
             * get bored of that injector, create a replacement injector
             * as a child of the long-lived injector.
             */
        }
    }
    

    Singleton bindings in the forever module will last as long as that module exists. Singleton bindings in the temporary module will last as long as you’re using the corresponding injector.

    Warning: By default, just-in-time bindings will be created in the top-level injector. If you need a binding to be short-lived, you need to bind both the interface and the implementation in your child injector’s module. That looks like this:

    public void configure() {
        bind(Foo.class).to(RealFoo.class);
        bind(RealFoo.class);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hoping that using something like this demo it is possible to drag items within
hoping to get some help here. I have locations on a database that I
Hoping some could direct me to a tool that i can use to monitor
Hoping someone can shed some light on this. I have a PHP program which
Hoping someone can provide some mysql advice... I have 2 tables that look like
Hoping that someone here will be able to provide some mysql advice... I am
Hoping I might be able to get some help with an infuriating problem which
Was hoping for some fresh eyes on this. I've got a page which will
Hoping that anybody here knows about a good one: I'm looking for a (free
Hoping some learned Rails developers here can recommend an existing Ruby on Rails plugin

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.