This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need basic @autowired info please

 
Tony Ennis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • 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
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will be a bean with the same name defined somewhere.
 
Narender Reddy Soma
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic