jQuery in Action, 3rd edition
The moose likes Other Application Frameworks and the fly likes spring - autowire= Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "spring - autowire="constructor"" Watch "spring - autowire="constructor"" New topic

spring - autowire="constructor"

Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143

I have a bean definition as follows in my spring-context.xml file:

<bean id="myService"
singleton="true" lazy-init="true" autowire="constructor"
dependency-check="default" >


public class MyServiceImpl extends BaseService implements MyService {

private AnotherService as;

public MyServiceImpl (SessionFactory sf, AnotherService as){
this.as = as;

'myService' bean is autowired by constructor.
what i understand by autowire='constructor' is :
create this bean instance using the constructor and arguments to the constructor are defined as other beans the spring-context.xml.
so i would have to write
in the bean definition

but how does the above code work without the <onstructor-arg>

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

That's the whole point of autowiring ! You don't need to tell the container which beans to use. It will look for a bean whose type is SessionFactory, and another bean whose type is AnotherService, and wire them to MyServiceImpl.

Note that if Spring finds more than one bean of type SessionFactory or AnotherService, it will throw an exception. In this case, you will have to set the constructor args explicitly with the constructor-arg tag.

[My Blog]
All roads lead to JavaRanch
Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143
Thanks Satou !
I agree. Here's the link: http://aspose.com/file-tools
subject: spring - autowire="constructor"
It's not a secret anymore!