aspose file tools*
The moose likes Spring and the fly likes need basic @autowired info please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "need basic @autowired info please" Watch "need basic @autowired info please" New topic
Author

need basic @autowired info please

Tony Ennis
Ranch Hand

Joined: May 22, 2003
Posts: 86
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

Joined: Sep 29, 2008
Posts: 12617

There will be a bean with the same name defined somewhere.
Narender Reddy Soma
Greenhorn

Joined: Jun 05, 2010
Posts: 23
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

Joined: May 22, 2003
Posts: 86
Thanks for the replies, I'll look for the bean definition again.
Tony Ennis
Ranch Hand

Joined: May 22, 2003
Posts: 86
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need basic @autowired info please