使用@Value注释时,Spring注入属性的方式到底如何? [英] How exactly does Spring inject properties when annotating with @Value?

查看:100
本文介绍了使用@Value注释时,Spring注入属性的方式到底如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道:当使用@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...?

提前谢谢!

推荐答案

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,尤其是

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆