• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown

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

Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4801
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
    Bookmark Topic Watch Topic
  • New Topic