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 ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
Ranch Hand
Posts: 199
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: 4577
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.
 
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic