Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Autowired for setter methods vs non-setter methods

Tags:

java

spring

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.

like image 780
dhblah Avatar asked May 12 '15 10:05

dhblah


Video Answer


2 Answers

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

like image 68
Ajinkya Avatar answered Oct 06 '22 13:10

Ajinkya


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.

like image 44
morsor Avatar answered Oct 06 '22 13:10

morsor