简体   繁体   中英

Spring @Autowired for setter methods vs non-setter methods

According to @Autowired javadoc :

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 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).

My questions are:

  1. What is meant by config methods ?

  2. 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?

  3. 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 property setter methods are effectively just a special case of such a general config method

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 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.

A config method is a factory-like method, which in this case would get the paramaters autowired:

@Autowired
public SomeObject initSomeObject(Object1 o1, Object2 o2, ...) {

@Autowired merely ensures that Spring will (attempt to) provide the needed parameters.

Config-method in this context refers to methods that you would specify under init-method or @PostConstruct

Setter as you already know is like setXXX

So obviously there is no difficulty in finding which is which. At the same time note that spring can not autowire based on parameter names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM