在春天工作的注释

时间:2015-11-06 08:38:06

标签: spring annotations

以下是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服务。如果错误,请纠正我。

1 个答案:

答案 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>