使用@Value注释时,Spring注入属性的方式到底如何? [英] How exactly does Spring inject properties when annotating with @Value?
问题描述
我一直想知道:当使用@Value
注释时,Spring如何准确地注入属性?检查字段是否具有注释的背后机制是什么?它是使用反射和某个类来找到所有带注释的类,并创建一个实例来注入属性,还是使用其他方法呢?我知道注释处理将仅在编译期间使用,并且不会更改代码,所以这里的幕后真的发生了什么??
I've been wondering: how exactly does Spring inject properties when using the @Value
annotation? What's the mechanism behind this that checks if a field has the annotation? Is it using reflection and some class that finds all annotated classes and creates an instance of them injecting the property, or is it doing it some other way? I know annotation processing would only be used during compilation and will not change the code, so what's happening behind the scenes here really...?
提前谢谢!
推荐答案
@Retention(value=RUNTIME)
注释,这意味着该信息在运行时可用(即使用反射).
The @Value
annotation type has the @Retention(value=RUNTIME)
annotation, which means that the information is available at runtime (i.e. using reflection).
A BeanPostProcessor
, in particular the AutowiredAnnotationBeanPostProcessor
can check for the presence of this annotation on fields, methods or constructors of a bean after instantiation.
这篇关于使用@Value注释时,Spring注入属性的方式到底如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!