Win a copy of Event Streams in Action 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 ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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

 
Ranch Hand
Posts: 186
2
  • 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.
 
Rancher
Posts: 4177
47
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!