Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

What gets evaluated by Spring first: The @Autowired annotations, or the @Bean annotations?

Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please consider the following two classes:

I followed an example which stated two contradictory flows and I would know to know which one is the correct one:

flow #1:  Everytime VideoSvc is instantiated, Spring looks at the @Configuration class (VideoConf) to find an implementation of StorageSystem and sets the variable 'storage' in the VideoSvc class to what the method annotated with @Bean returns.

flow #2: At run-time, the VideoConf class is instantiated, and an object is proactively created of type StorageSystem as the method annotated with @Bean is run. Then, Spring looks for any @Autowired variables to which this object can be assigned to. It finds the variable 'storage' in VideoSvc, and conveniently assigns the return type.

So which one is the correct flow? I'm inclined to think that it's wasteful to proactively create an instance of VideoConf and run this storageSystem() method,  and then try to find a variable to plug it into, and rather, it makes more sense to look at the variables first which are annotated with the @Autowired annotation, and then try to find an appropriate assignment for them... but I'm not too sure.
Posts: 4577
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Configuration is scanned as part of the component scanning, and the bean is created at that point.
I think this documentation is still relevant.

As to what order it handles the Configuration class or the Component class, I'm not sure it's defined.

As for how autowiring is done, that's handled during component scanning.
It's driven entirely by the component, not by finding a bean and then hunting for components that depend on that bean.
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic