26

I am learning Spring, and as far as I understand, when we use @annotation on a method which has a generic name (not a setter method), then the method's arguments are autowired.

public class MovieRecommender {

    private MovieCatalog movieCatalog;

    private CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public void prepare(MovieCatalog mC,
            CustomerPreferenceDao cPD) {
        this.movieCatalog = mC;
        this.customerPreferenceDao = cPD;
    }

    // ...

}

So, here, the fields movieCatalog and customerPreferenceDao are autowired with the values of mC and cPD. What I fail to understand is how is this different from the same method without the "@autowired".

I understand @autowired when applied to a Field name, but not able to understand when the values are explicitly being passed to the method (either a setter or any other method), then what does Spring do special?

Nishit
  • 1,276
  • 2
  • 11
  • 25
  • @Autowired can be used only on "a constructor, field, setter method or config method". See [this answer](http://stackoverflow.com/a/3746611/2747533) for details. – MirMasej Sep 03 '15 at 17:29
  • Possible duplicate of http://stackoverflow.com/questions/19414734/understanding-spring-autowired-usage – Milan Milanov Sep 03 '15 at 17:30

4 Answers4

37

quite late answer, but here it is:

any method annotated with @Autowired is a config method. It is called on bean instantiation after field injection is done. The arguments of the method are injected into the method on calling.

P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66
  • 2
    my question is why do we annotate a method with @Autowired ? What is the purpose of it? – vigamage Apr 14 '19 at 02:54
  • 13
    it's just an alternative to annotate the method with `@PostConstruct`; methods annotated with these annotations are called after bean creation and can be used to initialize the bean. `@PostConstruct` comes from JavaEE, whereas `@Autowired` is of Spring origin. – P.J.Meisch Apr 14 '19 at 09:05
  • 1
    very cool +1. But are there any preference/advantages/situations where i should prefer `@Autowired` instead of `@PostConstruct` for setting up configuations. – samshers Mar 12 '22 at 16:06
7

The @autowired method you have provided does not need to be invoked from within your own code. It will be invoked by the Spring framework. Imagine a setter method in a class that is declared as @autowired. The setters parameter is provided by Spring. So before using the class object instance you do not have to write code to call the setter method, just like you don't need to provide the parameters of an @autowired constructor. There are lots of things you can do with autowired. Check out these two examples:

Calling @autowired method

Other uses of @autowired

One of the advantages of @autowired is that you do not have to instantiate the object, as Spring Framework will do that for you. But not only that, an @autowired object is managed by the Spring Framework, so you don't have to worry about handling of object instances, cause Spring does it for you. So it saves a lot of coding and unnecessary code that is often used to keep track of and manage the objects. Hopefully this was of some help.

i-tms
  • 559
  • 5
  • 7
1

@autowired on a method is used for setter-injection. it is not different from field injection besides that the beans is not that dependent on the spring-container, you could instantiate and wire it yourself as well.

one reason to use it is if you have circular dependencies.

another use of setter injection is that it allow re-injection of (a possibly optional) dependency at a later time (JMX).

cproinger
  • 2,258
  • 1
  • 17
  • 33
-5
public class MovieRecommender {

    @Autowired
    private MovieCatalog movieCatalog;

    @Autowired
    private CustomerPreferenceDao customerPreferenceDao;

}

Now you have no need for a prepare method!

wesker317
  • 2,172
  • 1
  • 16
  • 11