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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:47:46+00:00 2026-05-29T09:47:46+00:00

Is there way to bind a method interceptor to a provider rather than an

  • 0

Is there way to bind a method interceptor to a provider rather than an instance?

e.g. I use the code below to bind interceptors how would I bind INTERCEPTOR to a provider and then to the annotation?

bindInterceptor(
    Matchers.any(), Matchers.annotatedWith(ANNOTATION.class), new INTERCEPTOR());
  • 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-29T09:47:47+00:00Added an answer on May 29, 2026 at 9:47 am

    Guice does not allow AOP on instances not built by Guice: Guice AOP Limitations

    “Instances must be created by Guice by an @Inject-annotated or no-argument constructor”

    This means that instances created with a provider will not be candidates for AOP.

    On the flip side, as long as your Provider is instantiated by Guice under the conditions mentioned, your Provider may be a candidate for AOP.

    Here’s an example that demonstrates this:

    AOP Annotation:

    @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)
    @interface AOPExample {}
    

    Provider:

    public class ExampleProvider implements Provider<Example> {
    
        @AOPExample
        public Example get() {
            System.out.println("Building...");
            return new Example();
        }
    }
    

    Target Example:

    public class Example {
    
        @AOPExample
        public void tryMe() {
            System.out.println("example working...");
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    }
    

    Module:

    public class ExampleModule extends AbstractModule {
        @Override
        protected void configure() {
            bindInterceptor(Matchers.any(), Matchers.annotatedWith(AOPExample.class), new LoggingAOP());
    
            bind(Example.class).toProvider(ExampleProvider.class);
        }
    }
    

    Test Code:

    public class Test {
    
        public static void main(String[] args) {
            Injector injector = Guice.createInjector(new TestModule());
    
            ExampleProvider exampleProvider = injector.getInstance(ExampleProvider.class);
            Example example = exampleProvider.get();
    
            example.tryMe();
    
            Example directExample = injector.getInstance(Example.class);
    
            directExample.tryMe();
    
        }
    }
    

    Test Output:

    start
    Building...
    end took: 3
    example working...
    start
    Building...
    end took: 0
    example working...
    

    Notice that the “example working…” is not surrounded by the timer code. The Provider.get (“Building…”) is however.

    If your question is: can the interceptor (new INTERCEPTOR()) be provided through a Guice Provider, the answer is no. The closest you may get to this functionality is calling the requestInjection() in the module configure method. This will inject your Interceptor with the appropriate code. From your interceptor you may be able to use Providers to avoid any sort of overhead that is causing you slowness during startup.

    Here’s what I mean:

    Module:

    public class TestModule extends AbstractModule {
        @Override
        protected void configure() {
            bind(String.class).toInstance("One");
            bind(String.class).annotatedWith(Names.named("two")).toInstance("Two");
    
            LoggingAOP loggingAOP = new LoggingAOP();
    
            bindInterceptor(Matchers.any(), Matchers.annotatedWith(AOPExample.class), loggingAOP);
    
            requestInjection(loggingAOP);
    
            bind(Example.class).toProvider(ExampleProvider.class);
        }
    }
    

    Interceptor:

    public class LoggingAOP implements MethodInterceptor {
    
        @Inject
        private Provider<SomethingThatTakesALongTimeToInit> provider;
    
        public Object invoke(MethodInvocation invocation) throws Throwable {
            provider.get()...
            System.out.println("start");
            long start = System.currentTimeMillis();
            Object value =  invocation.proceed();
            System.out.println("end took: " + (System.currentTimeMillis() - start));
            return value;
        }
    }
    

    Hope this answers your question.

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

Sidebar

Related Questions

Is there a way to bind properties from one instance of a class to
In Python, is there a way to bind an unbound method without calling it?
Is there a way to bind a MemoryStream to asp:image control?
Is there a way to bind a Generic List to a multicolumn listbox, yes
Is there a way to bind the child properties of an object to datagridview?
Is there a way to fire a two way data bind when the key
Is there a standard way to bind arrays (of scalars) in a SQL query?
I was wondering if there was a way to bind an ArrayList (or any
Is there a super quick way to bind to the inverse of a boolean
does anyone know if there is a simple way to bind a textblock to

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.