LaVOZs

The World’s Largest Online Community for Developers

'; Defining a template for custom annotations in Java for dependency injection - LavOzs.Com

I want to define custom annotations for a set of Strings that I will inject as class fields. Currently, my Spring IoC container class looks like this-

@Configuration
public class DependencyConfig {

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Qualifier
    public @interface FooInterface {}

    @FooInterface
    public String foo() { return "Foo" }

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Qualifier
    public @interface BarInterface {}

    @BarInterface
    public String foo() { return "Bar" }

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Qualifier
    public @interface FooBarInterface {}

    @FooBarInterface
    public String foo() { return "FooBar" }
}

I don't like that I have to separately define all the annotations with the same attributes. Is there a way to shorten this code but still have individual annotation types like @Foo, @Bar and @FooBar?

Related
What is dependency injection?
What are the benefits of dependency injection containers?
Dependency Injection vs Factory Pattern
How to use log4net with Dependency Injection
Injecting Mockito mocks into a Spring bean
Which @NotNull Java annotation should I use?
Java Dependency injection: XML or annotations
Inversion of Control vs Dependency Injection
Why does one use dependency injection?