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 3676824
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:08:48+00:00 2026-05-19T03:08:48+00:00

Let’s say I have a Closeable object injected through Guice using request scope: @Provides

  • 0

Let’s say I have a Closeable object injected through Guice using request scope:

@Provides @RequestScoped
public MyCloseableResource providesMyCloseableResource(){
  return new MyCloseableResourceImpl();
}

Is it possible to hook-up a clean-up method that would automatically call close() on my resource when the scope exists, without resorting to custom scope implementation?

Looking at the custom scope implementation guide on the Guice wiki, it shows that scopes should be created and cleaned up like this:

/**
 * Runs {@code runnable} in batch scope.
 */
public void scopeRunnable(Runnable runnable) {
  scope.enter();
  try {
    // explicitly seed some seed objects...
    scope.seed(Key.get(SomeObject.class), someObject);
    // create and access scoped objects
    runnable.run();
  } finally {
    scope.exit();
  }
}

I am wondering if there is way to hook-up some custom clean-up code in the finally of the built-in scopes (especially session and request scopes).

If it isn’t possible, might there be issues that would discourage this kind of automatic clean-up?

I have found ways of achieving the same effect in servlet containers by implementing a Filter to create and clean-up a resource per request, which works great but I am curious if it is possibly with pure Guice.

  • 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-19T03:08:48+00:00Added an answer on May 19, 2026 at 3:08 am

    I faced a similar problem myself and finally rolled a Disposable interface which offers nothing but a public void dispose() method. I find this especially valuable for classes that register listeners somewhere and need to unregister them at a defined time. What I already had was my AttributeHolderScope which I blogged about so I won’t repeat that part here. The only thing that is missing now is the AbstractAttributeHolder which looks like this:

    /**
     * An anstract base class for implementing the {@link AttributeHolder}
     * interface which has an implementation of the attribute related methods.
     *
     * @author Matthias Treydte <waldheinz at gmail.com>
     */
    public abstract class AbstractAttributeHolder
            implements AttributeHolder, Disposable {
    
        private final Object lock = new Object();
        private transient Map<Object, Object> attributes;
    
        public AbstractAttributeHolder() {
            this.attributes = new HashMap<Object, Object>();
        }
    
        public void replaceAttributes(Map<Object, Object> newAttr) {
            synchronized (getAttributeLock()){
                this.attributes = newAttr;
            }
        }
    
        @Override
        public Object getAttributeLock() {
            return this.lock;
        }
    
        @Override
        public final void putAttribute(Object key, Object value) {
            synchronized (getAttributeLock()) {
                attributes.put(key, value);
            }
        }
    
        @Override
        public final boolean hasAttribute(Object key) {
            synchronized (getAttributeLock()) {
                return attributes.containsKey(key);
            }
        }
    
        @Override
        public final Object getAttribute(Object key) {
            synchronized (getAttributeLock()) {
                return attributes.get(key);
            }
        }
    
        @Override
        public final Set<Object> getAttributes() {
            synchronized (getAttributeLock()) {
                return Collections.unmodifiableSet(
                        new HashSet<Object>(this.attributes.values()));
            }
        }
    
        @Override
        public void dispose() {
            synchronized (this.getAttributeLock()) {
                for (Object o : this.attributes.values()) {
                    if (o instanceof Disposable) {
                        final Disposable d = (Disposable) o;
                        d.dispose();
                    }
                }
    
                this.attributes.clear();
            }
        }
    }
    

    This class itself implements Disposable so you can have nested scopes and when you dispose an outer scope, all nested scopes and, more importantly, all injected instances that implement Disposable get cleaned up. And to precisely answer you question: I don’t think that this is possible with the Scope implementations provided by Guice itself, but it can be done. Everytime I look at this code I ask myself if this can’t be done in a more concise way, but then it works beautifully (at least for me).

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

Sidebar

Related Questions

Let's say I have an facebook application running using the JS SDK. First user
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say I have the following classes : public class MyProductCode { private String
Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I have an Instant Messenger server using SignalR. I want to broadcast
Let's say we have the following: abstract class A; class B : public A;
Let's say we have: @Entity public class Order { @Id private int id; @OneToMany(mappedBy=order)
Let us say I have a Service public interface IAreaService { int CalculateArea(int x,int
Let's say I don't have photoshop, but I want to make pattern files (.pat)

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.