注解
注解本身包含处理逻辑,但是用户可以获取注解,并执行相关逻辑。
Bean的注解有以下3种方式:
- @Autowire 默认byName,可以与 @Qualifier 一起使用,指定类型
// org.springframework.beans.factory.annotation.Autowired spring的注解,可以与Qualifier使用指定注入的类型 @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Autowired { /** * Declares whether the annotated dependency is required. * <p>Defaults to {@code true}. */ boolean required() default true; }
2)@Resource
//javax.annotation.Resource JSR-250 @Target({TYPE, FIELD, METHOD}) @Retention(RUNTIME) public @interface Resource { /** *The JNDI name of the resource. */ String name() default ""; String lookup() default ""; Class<?> type() default java.lang.Object.class; enum AuthenticationType { CONTAINER, APPLICATION } AuthenticationType authenticationType() default AuthenticationType.CONTAINER; boolean shareable() default true; String mappedName() default ""; String description() default "";
3)@Inject
//JSR-330 的标准注解,javax.inject.Inject; @Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Inject { }