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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:51:29+00:00 2026-06-04T09:51:29+00:00

I have a situation where Guice is working for some bindings, and not at

  • 0

I have a situation where Guice is working for some bindings, and not at all for others. Clearly I am using the API incorrectly.

In part, it’s probably because I’m trying to get too "fancy" with how I’m using Guice. I’ve created an inheritance tree of Modules and I think I’ve gotten too clever (or foolish!) for my own good.

Before you look at the code below, just please understand my intention, which was to provide a reusable Module that I can place in a JAR and share across multiple projects. This abstract, reusable Module would provide so-called "default bindings" that any implementing Module would automatically honor. Things like an AOP MethodInterceptor called Profiler, which looks for methods annotated with @Calibrated and automatically logs how long it took for that method to execute, etc.

Observe the following:

@Target({ ElementType.METHOD })
@RetentionPolicy(RetentionPolicy.RUNTIME)
@BindingAnnotation
public @interface Calibrated{}

public class Profiler implement MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation arg0) throws Throwable {
        // Eventually it will calculate and log the amount of time
        // it takes an intercepted method to execute, hence "Profiler".
        System.out.println("Intercepted!");
        return arg0.proceed();
    }
}

public abstract class BaseModule implements Module {
    private Binder binder;

    public BaseModule() {
        super();
    }

    public abstract void bindDependencies();

    @Override
    public void configure(Binder bind) {
        // Save the binder Guice passes so our subclasses can reuse it.
        setBinder(bind);

        // TODO: For now do nothing, down the road add some
        // "default bindings" here.

        // Now let subclasses define their own bindings.
        bindDependencies();
    }

    // getter and setter for "binder" field
    // ...
}

public abstract class AbstractAppModule extends BaseModule {
    /* Guice Injector. */
    private Injector injector;

    // Some other fields (unimportant for this code snippet)

    public AbstractAppModule() {
        super();
    }

    // getters and setters for all fields
    // ...

    public Object inject(Class<?> classKey) {
        if(injector == null)
            injector = Guice.createInjector(this);

        return injector.getInstance(classKey);
    }
}

So, to use this small library:

public class DummyObj {
    private int nonsenseInteger = -1;

    // getter & setter for nonsenseInteger

    @Calibrated
   public void shouldBeIntercepted() {
       System.out.println("I have been intercepted.");
   }
}

public class MyAppModule extends AbstractAppModule {
    private Profiler profiler;

    // getter and setter for profiler

    @Override
    public void bindDependencies() {
        DummyObj dummy = new DummyObj();
        dummy.setNonsenseInteger(29);

        // When asked for a DummyObj.class, return this instance.
        getBinder().bind(DummyObj.class).toInstance(dummy);

        // When a method is @Calibrated, intercept it with the given profiler.
        getBinder().bindInterceptor(Matchers.any(),
            Matchers.annotatedWith(Calibrated.class),
            profiler);
    }
}

public class TestGuice {
    public static void main(String[] args) {
        Profiler profiler = new Profiler();
        MyAppModule mod = new MyAppModule();
        mod.setProfiler(profiler);

        // Should return the bounded instance.
        DummyObj dummy = (DummyObj.class)mod.inject(DummyObj.class);

        // Should be intercepted...
        dummy.shouldBeIntercepted();

        System.out.println(dummy.getNonsenseInteger());
    }
}

This is a lot of code so I may have introduced a few typos when keying it all in, but I assure you this code compiles and throws no exceptions when ran.

Here’s what happens:

  • The @Calibrated shouldBeIntercepted() method is not intercepted; but…
  • The console output shows the dummy’s nonsense integer as…29!!!!

So, regardless of how poor a design you may think this is, you can’t argue that Guice is indeed working for 1 binding (the instance binding), but not the AOP method interception.

If the instance binding wasn’t working, then I would happily revisit my design. But something else is going on here. I’m wondering if my inheritance tree is throwing the Binder off somehow?

And I’ve verified that I am binding the interceptor to the annotation correctly: I created another package where I just implement Module (instead of this inheritance tree) and use the same annotation, the same Profiler, and it works perfectly fine.

I’ve used Injector.getAllBindings() to print out the map of all my MyAppModule‘s bindings as Strings. Nothing is cropping up as the clear source of this bug.

  • 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-04T09:51:31+00:00Added an answer on June 4, 2026 at 9:51 am

    Interception only works on Objects created by Guice (see “Limitations” http://code.google.com/p/google-guice/wiki/AOP#Limitations). You are using “new” to create the DummyObj, so no matter how clever your Module is Set up, the instance is created Outside guice.

    Here’s a little snipplet based on your coding. (I use your Calibrated Annotation, but had everything else in one class. You should have a look at “AbstractModule”. It saves a lot of manual stuff you did with your Module-Hierarchy.

    public class MyModule extends AbstractModule implements MethodInterceptor {
    
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
    
        System.out.println("Intercepted@invoke!");
    
        return methodInvocation.proceed();
    }
    
    @Override
    protected void configure() {
        bind(Integer.class).annotatedWith(Names.named("nonsense")).toInstance(29);
        bindInterceptor(Matchers.any(), Matchers.annotatedWith(Calibrated.class), this);
    }
    
    public static void main(String... args) {
        Dummy dummy = Guice.createInjector(new MyModule()).getInstance(Dummy.class);
    
        dummy.doSomething();
    
        System.out.println(dummy.getNonsense());
    }
    }
    

    And my Dummy:

    public class Dummy {
    
    @Inject
    @Named("nonsense")
    private int nonsense;
    
    
    public int getNonsense() {
        return nonsense;
    }
    
    
    public void setNonsense(int nonsense) {
        this.nonsense = nonsense;
    }
    
    @Calibrated
    public void doSomething() {
        System.out.println("I have been intercepted!");
    }
    }
    

    So you see? I never use the word “new” (except for the Module ….). I let Guice handle the Dummy-Object and just configure the value for the nonsense int, which is then injected.

    Output:

    Intercepted@invoke!
    I have been intercepted!
    29
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have situation where say we have four engineers that are working on software
I have a situation in which a managed DLL calls some unmanaged DLL. I
I have a situation where I need to use some strings temporarily but I've
I have situation when the current directory becomes invalid (ie, when some program deletes
How to write right in this situation: I have some method, that return NSMutableArray*.
I have a situation where I need to connect multiple DBs to get all
I have a situation where I need to use Guice 3.0 to instantiate my
I have some trouble handling custom C++ exceptions when calling from Cython. My situation
i have some simple situation, but for me, it looks like very tough at
i have situation like this: class IData { virtual void get() = 0; virtual

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.