Spring @Autowired for setter方法与非setter方法 [英] Spring @Autowired for setter methods vs non-setter methods

查看:254
本文介绍了Spring @Autowired for setter方法与非setter方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 @Autowired javadoc


将构造函数,字段,setter方法或配置方法标记为自动装配
通过Spring的依赖注入设施。任何给定bean类只有一个构造函数(最大
)可以带有这个注释,指示
构造函数在用作Spring bean时要自动装配。这样的构造函数
不必公开。在调用任何配置方法之前,在
构造bean之后立即注入字段。这样的
配置字段不必是公开的。配置方法可以有
任意名称和任意数量的参数;每个参数
将与Spring容器中的匹配bean一起自动装配。

Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities. Only one constructor (at max) of any given bean class may carry this annotation, indicating the constructor to autowire when used as a Spring bean. Such a 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属性设置器方法实际上只是这种
常规配置方法的特例。这种配置方法不必是公开的。
在多参数方法的情况下,'required'参数是
适用于所有参数。如果是Collection或Map
依赖类型,容器将自动装配与
声明的值类型匹配的所有bean。如果是Map,则必须将键声明为
type String,并将其解析为相应的bean名称。注意
实际注入是通过BeanPostProcessor执行的,而
反过来意味着你不能使用@Autowired将引用注入
BeanPostProcessor或BeanFactoryPostProcessor类型。请参考
autowiredAnnotationBeanPostProcessor类的javadoc(默认情况下,
检查是否存在此注释)。

Bean property setter methods are effectively just a special case of such a general config method. Such config methods do not have to be public. In the case of multiple argument methods, the 'required' parameter is applicable for all arguments. In case of a Collection or Map dependency type, the container will autowire all beans matching the declared value type. In case of a Map, the keys must be declared as type String and will be resolved to the corresponding bean names. Note that actual injection is performed through a BeanPostProcessor which in turn means that you cannot use @Autowired to inject references into BeanPostProcessor or BeanFactoryPostProcessor types. Please consult the javadoc for the AutowiredAnnotationBeanPostProcessor class (which, by default, checks for the presence of this annotation).

我的问题是:


  1. 配置方法是什么意思?

而且,假设我有一个带有 @Autowired 的setter方法和一些带有<的任意方法code> @Autowired 。我假设在bean实例化之后,spring会自动调用setter方法,而不会调用随机命名的 @Autowired 方法,对吗?

And also, let's say I have a setter method with @Autowired and some arbitrary methods with @Autowired. I assume that setter method is invoked by spring automatically after the bean instantiation, while random-named @Autowired methods won't be invoked, am I right?

春天如何理解在实例化(setter)之后应该调用哪个 @Autowired 方法,而其他人不应该?这与javadoc的声明如何相关,说:

Also how does spring understand which @Autowired method should be invoked after the instantiation (setters), while others shouldn't? And how does this correlate with a statement from javadoc, saying that:




Bean属性setter方法实际上只是
这种一般配置方法的特例

Bean property setter methods are effectively just a special case of such a general config method

最后一个问题:我在哪里可以阅读它吗?因为spring文档没有任何关于它的信息,我无法在其javadoc中找到spring使用的确切逻辑。

One final question: where I can read about it? since spring documentation doesn't have any information on that and I wasn't able to find the exact logic used by spring in its javadoc.

推荐答案

@Autowired 注释可与构造函数,setter方法或任何其他方法。
每当Spring找到 @Autowired 注释时,它将尝试查找与方法参数匹配的bean,并将调用该方法。如果多个方法(setter或non-setter)具有 @Autowired 注释,那么在bean实例化之后Spring将调用所有方法。

@Autowired annotation can be used with constructor, setter method or just any other method. Whenever Spring finds @Autowired annotation it will try to find beans matching to method parameters and will invoke that method. If multiple methods (setter or non-setter) have @Autowired annotation, all will be invoked by Spring after bean instantiation.

这篇关于Spring @Autowired for setter方法与非setter方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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