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

  • SEARCH
  • Home
  • 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 9031921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:51:07+00:00 2026-06-16T07:51:07+00:00

Is there an easy way to enumerate all the singleton instances already created by

  • 0

Is there an easy way to enumerate all the singleton instances already created by a Guice Injector? Or alternately a way to get all singletons that implement a specific interface?

I would like to find all singleton instances that implement java.io.Closeable so I can close them cleanly when my service is shut down.

  • 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-06-16T07:51:08+00:00Added an answer on June 16, 2026 at 7:51 am

    This would be fairly easy to write using Guice’s SPI. Guice’s Injector instance exposes a getAllBindings() method that lets you iterate through all of the bindings.

    // Untested code. May need massaging.
    private void closeAll(Injector injector) {
      for(Map.Entry<Key<?>, Binding<?>> entry : injector.getAllBindings().entrySet()) {
        final Binding<?> binding = entry.getValue();
        if (Closeable.class.isAssignableFrom(
            entry.getKey().getTypeLiteral().getRawType())) {
          binding.accept(new DefaultBindingScopingVisitor<Void>() {
            @Override public Void visitEagerSingleton() {
              Closeable instance = (Closeable) (binding.getProvider().get());
              try {
                instance.close();
              } catch (IOException e) {
                // log this?
              }
              return null;
            }
          });
        }
      }
    }
    

    Note that I only overrode visitEagerSingleton and that you may have to modify the above to handle lazily-instantiated @Singleton instances with implicit bindings. Also note that if you bind(SomeInterface.class).to(SomeClosable.class).in(Singleton.class) you may need to make the SomeInterface.class Closable, though you could also instantiate every Singleton (by putting the Closable check inside the scope visitor) to determine if the provided instance itself is Closable regardless of the key. You may also be able to use Reflection on the Binding’s Key to check whether the type is assignable to Closable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easy way to calculate the derivative of non-liner functions that are
Is there an easy way to bit-reflect a byte variable in Delphi so that
In .NET is there a way to enumerate all the values for \w? As
Is there easy way to get the hash tag in script source? Example: <script
Is there an easy way to approximate the points (if any) where two instances
Is there an easy way to tell if a ruby script is already running
Is there an easy way to log PHP errors to a log for a
Is there an easy way to generate docs for REST api direct from a
Is there an easy way of getting the datetime of the last change to
Is there an easy way in javascript to only escape certain ranges of control

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.