简体   繁体   English

注释类型声明中的@Autowired用法

[英]@Autowired usage in Annotation type declaration

I understand @Autowired used for dependency injection 我了解@Autowired用于依赖项注入

Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities. 将构造函数,字段,setter方法或config方法标记为由Spring的依赖项注入工具自动装配。

But what's the usage for Annotation type declaration that is defined 但是定义的注释类型声明有什么用

 @Target(value={CONSTRUCTOR,METHOD,PARAMETER,FIELD,ANNOTATION_TYPE}) 

Is there any usage/effect for defining @Autowired in custom annotation: 在自定义注释中定义@Autowired是否有任何用法/效果:

@Autowired
public @interface MyAnnotation {
}

I didn't find any references in Spring docs 我在Spring文档中找不到任何参考

By default, the autowiring will fail whenever zero candidate beans are available; 默认情况下,每当有零个候选bean可用时,自动装配都会失败。 the default behavior is to treat annotated methods, constructors, and fields as indicating required dependencies. 默认行为是将带注释的方法,构造函数和字段视为指示所需的依赖项。

If you place @Autowired annotation above other annotation declaration, the new annotation will have the same effect as @Autowired annotation. 如果将@Autowired注释放在其他注释声明的上方,则新注释将与@Autowired注释具有相同的效果。 For example, in your case you can use @MyAnnotation above constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities. 例如,在您的情况下,可以在构造函数,字段,setter方法或config方法上方使用@MyAnnotation ,以通过Spring的依赖项注入工具自动进行连接。

Moreover, you may discover, that @Controller annotation in spring framework declared as @Component . 此外,您可能会发现spring框架中的@Controller注释声明为@Component As result, classes that marked as @Controller also created as container beans. 结果,标记为@Controller类也创建为容器bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM