OneMoreError
OneMoreError

Reputation: 7738

Inheriting the annotations of an interface in Java

I have a case where by I want all the classes that implement a particular interface to also inherit the annotations of the class, as well as the methods.

@Component
public interface ITask {
    @Scheduled(fixedRate = 5000)
    void execute();
}

public class TaskOne implements ITask {
    private static Logger LOGGER = LoggerFactory.getLogger(TaskOne.class);
    @Override
    public void execute() {
        LOGGER.info("Hello from task one");
    }
}

public class TaskTwo implements ITask {
    private static Logger LOGGER = LoggerFactory.getLogger(TaskTwo.class);
    @Override
    public void execute() {
        LOGGER.info("Hello from task two");
    }
}

So, I wanted both the tasks to be treated as a bean, as they are implementing an interface that is a bean. Also, I was hoping that the execute() methods of both the tasks would be scheduled at every 5 seconds. I have used the annotation @EnableScheduling with the main Application class containing the main() method.

Instead, I have to do this to make it execute in a periodic manner :

public interface ITask {
    void execute();
}

@Component
public class TaskOne implements ITask {
    private static Logger LOGGER = LoggerFactory.getLogger(TaskOne.class);
    @Override
    @Scheduled(fixedRate = 5000)
    public void execute() {
        LOGGER.info("Hello from task one");
    }
}

@Component
public class TaskTwo implements ITask {
    private static Logger LOGGER = LoggerFactory.getLogger(TaskTwo.class);
    @Override
    @Scheduled(fixedRate = 5000)
    public void execute() {
        LOGGER.info("Hello from task two");
    }
}

I don't want want to annotatae every task with this @Component annotation, and the @Scheduled annotation for every execute method. I was hoping to provide some default value for @Scheduled, and that the @Component annotation can be taken care of by implementing a certain interface.

Is it possible in Java ?

Upvotes: 0

Views: 4164

Answers (1)

Sandeep Kaul
Sandeep Kaul

Reputation: 3267

This is not possible is Java.

You'll have to annotate different classes separately.

But if this is a custom annotation being talked about, you could mark the annotation with @Inherited and put it on a base class, and have all your other classes extend the base class.

Anyway, you cannot annotate @Component or @Scheduled with @Inherited, so in this use case this solution would not work.

Why is this not allowed(from another answer here):

I'd say the reason is that otherwise a multiple-inheritance problem would occur.

Example:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) @Inherited
public @interface Baz { String value(); }

public interface Foo{
    @Baz("baz") void doStuff();
}

public interface Bar{
    @Baz("phleem") void doStuff();
}

public class Flipp{
    @Baz("flopp") public void doStuff(){}
}

public class MyClass extends Flipp implements Foo, Bar{}

If I do this:

MyClass.class.getMethod("doStuff").getAnnotation(Baz.class).value()

what's the result going to be? 'baz', 'phleem' or 'flopp'?

Upvotes: 2

Related Questions