以下是Spring的服务@interface
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
String value() default "";
}
使用@Component进行注释。以下是组件
的@interface@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component {
}
如果@Component已经拥有它,为什么给@Retention和@Target服务。如果错误,请纠正我。
答案 0 :(得分:1)
这不是真正的Spring,而是Java本身。注释@A
上的注释(例如@Target
)不适用于@B
,只是因为@B
注释了@A
。
您可以通过简单的实验验证此行为:
@Target(ElementType.TYPE)
public @interface A {}
@A
public @interface B {}
public class Test {
@B private String foo;
}
如果@B
注释从@Target
获得@A
,那么此代码将无法编译,因为@B
不适用于字段,但确实如此。< / p>