Search code examples
javaguiceprovider

Provider for custom annotation


How with Google Guice create provider for custom annotations.

@SuperAnno Object test - my custom annotation

If this annotation find, system automatically call provider for convert


Solution

  • In your Guice module:

    @Provides
    @SuperAnno
    Object get() {
        return MyObject.getInstance(); // for example
    }
    

    In your main class:

    @Inject
    @SuperAnno
    Object injectedObject;
    
    public doStuff() {
        // do stuff with injected field
    }
    

    I would recommend using a more specific type than Object. Your question is not entirely clear to me so I used Object since it seems like you wanted to. Please explain question further if this does not answer it.