aspose file tools*
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
Author

spring - autowire="constructor"

Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143
Hi,

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

<bean id="myService"
class="com.try.services.MyServiceImpl"
singleton="true" lazy-init="true" autowire="constructor"
dependency-check="default" >
</bean>

and

public class MyServiceImpl extends BaseService implements MyService {

private AnotherService as;

public MyServiceImpl (SessionFactory sf, AnotherService as){
super(sf);
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
<constructor-arg>..........</constructor-arg>
in the bean definition

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

thanks,
Gayatri
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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"