简体   繁体   中英

annotating methods in implementation classes or interfaces?

I am using Java1.7 and spring3.

I have below classes.

MyInterface.java

public interface MyInterface{

  String getResult();

}

MyInterfaceImpl.java

public class MyInterfaceImpl implements MyInterface{

  @MyCustomAnnotation
  public String getResult(){
  //some logic

   }

}

I annotated method in Impl class. Is it good practice to annotate methods in implementation class? Or do i need to annotate methods in the interface itself?

Thanks!

Classes don't inherit the annotation from interfaces, so you should be really careful using annotations on interfaces.

Here is an example of what can happen if you are not careful:

http://kim.saabye-pedersen.org/2013/05/spring-annotation-on-interface-or-class.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM