有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java@Autowired在Spring中是如何实现的

我真的很想对@autowired有一个基本的了解 春季实施
反射应该以某种方式隐含在它的实现中,但我不知道如何实现
你能帮忙吗


共 (1) 个答案

  1. # 1 楼答案

    通过@Autowired的自动连线是由BeanPostProcessor实现执行的,特别是^{}

    这个BeanPostProcessor处理每个bean,将扫描其类(和超类)中的任何@Autowired注释,并根据注释是什么(构造函数、字段或方法),采取适当的操作

    对于建设者

    Only one constructor (at max) of any given bean class may carry this annotation with the 'required' parameter set to true, indicating the constructor to autowire when used as a Spring bean. If multiple non-required constructors carry the annotation, they will be considered as candidates for autowiring. The constructor with the greatest number of dependencies that can be satisfied by matching beans in the Spring container will be chosen. If none of the candidates can be satisfied, then a default constructor (if present) will be used. An annotated constructor does not have to be public.

    田里

    Fields are injected right after construction of a bean, before any config methods are invoked. Such a config field does not have to be public.

    方法

    Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container. Bean property setter methods are effectively just a special case of such a general config method. Config methods do not have to be public.

    所有这些都是通过反思来完成的

    进一步阅读: