【问题标题】:annotating methods in implementation classes or interfaces?在实现类或接口中注释方法?
【发布时间】:2023-03-23 19:18:01
【问题描述】:

我使用的是Java1.7和spring3。

我有以下课程。

MyInterface.java

public interface MyInterface{

  String getResult();

}

MyInterfaceImpl.java

public class MyInterfaceImpl implements MyInterface{

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

   }

}

我在 Impl 类中注释了方法。在实现类中注释方法是一种好习惯吗?或者我需要在接口本身中注释方法吗?

谢谢!

【问题讨论】:

标签: java spring annotations


【解决方案1】:

类不会从接口继承注解,因此在接口上使用注解时应该非常小心。

下面是一个例子,说明如果你不小心会发生什么:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2010-10-23
  • 2013-12-30
  • 2011-03-08
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 2014-04-12
相关资源
最近更新 更多