java - @Autowired 在注解类型声明中的用法

标签 java spring annotations autowired spring-annotations

我明白了@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/

相关文章:

java - 在 HashMap 中声明 lambda 的正确方法

java - JSP代码的复用

java - 无法理解java中split的使用

java - 关于 CsrfPreventionFilter 的 tomcat 错误是什么意思?

spring - 覆盖 PropertyPlaceholderConfigurer 中的属性值

java - 类型注释属于有界通配符的什么位置?

java - 从 eclipse 执行时 Spring 4.1.7 + Junit 4.12 不工作

java - Spring应用服务器通过websocket向客户端传输数据

java - 有没有一种方法可以在围绕连接点的 AOP 之前进行一次性处理,以使我的方面性能更高?

java - 如何使用反射在运行时更改注释值?