Resource和Autowire其实用谁都可以,只要了解下注入方式和可标注类型即可
Resource
@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)
public @interface Resource {
String name() default "";
Class<?> type() default java.lang.Object.class;
...
}
jdk提供,默认按名称注入(找不到再按类型注入)。
可作用在类,字段,方法
Autowire
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
boolean required() default true;
}
spring提供,默认按类型注入(找不到再按名称注入)。如果有多个,配合@Qualify(name=“”)指定名称使用(当然注入bean的时候可以使用@Primary)。
可作用在字段,方法,构造方法,注解