Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

need basic @autowired info please

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've inherited (another!) application. This one uses Spring. I have never used Spring. I'm reading lots of sites, but none seem to tell me what @autowired does in a way I can understand it.

For example, some code has this:



Now I see the interface code, and I see the implementation's code. The Spring config files do not refer to either class explicitly. I see the 'context:component-scan' XML configuration that allows Spring to 'hunt' for interesting classes.

What I am not getting is what the @Autowire is actually doing. I believe it is causing the instantiation of someInterfaceImpl to happen. But how does it know to instantiate the proper class? What if I had another implementation of SomeInterface? How would it choose?

Sorry for the newb question
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There will be a bean with the same name defined somewhere.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Ennis wrote:I've inherited (another!) application. This one uses Spring. I have never used Spring. I'm reading lots of sites, but none seem to tell me what @autowired does in a way I can understand it.

For example, some code has this:



Now I see the interface code, and I see the implementation's code. The Spring config files do not refer to either class explicitly. I see the 'context:component-scan' XML configuration that allows Spring to 'hunt' for interesting classes.

What I am not getting is what the @Autowire is actually doing. I believe it is causing the instantiation of someInterfaceImpl to happen. But how does it know to instantiate the proper class? What if I had another implementation of SomeInterface? How would it choose?

Sorry for the newb question



Hi,
@Autowired wires the appropriate bean instance to the dependent class composite instance variable. The qualification of bean instance could be either bean name / type etc. By deafult it is by name though you can change it.

and coming to your question, how if there are two implementations for same interface ..May be you should think of getting all eligible beans like this

@Autowired
public void setIImpl(Map<String, IImpl> iImpl) {
this.iImpl = iImpl;
}

or

may be can think of configuring a factory.






 
Tony Ennis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies, I'll look for the bean definition again.
 
Tony Ennis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There will be a bean with the same name defined somewhere.



Just got back on this project. There is no mapping for the concrete class. I was able to talk to the developer, he says Spring just knows what to do.

I suppose of there's only one class that implements the interface this is quite possible.

Unsatisfying.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic