This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

spring - autowire="constructor"

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gayatri Ganesh
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Satou !
 
Acetylsalicylic acid is aspirin. This could be handy too:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!