我明白了@Autowired用于依赖注入(inject)
Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.
但是定义的Annotation类型声明有什么用
@Target(value={CONSTRUCTOR,METHOD,PARAMETER,FIELD,ANNOTATION_TYPE})
在自定义注解中定义@Autowired
有没有什么用法/效果:
@Autowired
public @interface MyAnnotation {
}
我在 Spring docs 中没有找到任何引用
By default, the autowiring will fail whenever zero candidate beans are available; the default behavior is to treat annotated methods, constructors, and fields as indicating required dependencies.
最佳答案
如果将@Autowired
注释放置在其他注释声明之上,则新注释将与@Autowired
注释具有相同的效果。例如,在您的情况下,您可以在构造函数、字段、setter 方法或配置方法上方使用 @MyAnnotation
,以便由 Spring 的依赖注入(inject)工具 Autowiring 。
此外,您可能会发现,Spring框架中的@Controller
注释被声明为@Component
。因此,标记为 @Controller
的类也创建为容器 bean。
关于java - @Autowired 在注解类型声明中的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56129191/