@Autowired源码
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, 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;
}
其中@Target中有写他可以支持的地方
- ElementType.CONSTRUCTOR 构造函数
- ElementType.METHOD 方法
- ElementType.PARAMETER 参数
- ElementType.FIELD 字段声明(包括枚举常量)
- ElementType.ANNOTATION_TYPE 注解类型声明
这里我们用到@Autowired的ElementType.FIELD注入到Map上
public interface OrderService {
}
@Service
public class MeituanOrderServiceImpl implements OrderService{
}
@Service
public class ElemeOrderServiceImpl implements OrderService{
}
@SpringBootApplication
public class DemoApplication {
@Autowired
public Map<String, OrderService> map;
@Bean
public void test(){
System.out.println(map);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Debug一下