• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Spring: Configuring bean property

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am following the PDF tutorial for Spring MVC that came along with Spring 2.5 bundle.
This may seem to be a naive question,
But I have following bean declaration in my applicationContext.xml



What I expected by this declaration is that, (in crudest terms) the class JdbcProductDao should contain a variable of type dataSourcewhich will have methods setDataSource() and getDataSource().

But class JdbcProductDao is as shown below,



SimpleJdbcDaoSupport doesn't have a field of type DataSource.
So how come the a property of "dataSource" is set for JdbcProductDao

[Edited to add 1 more question]

From what I gathered about Spring is that every Class in Spring is a bean.So setters and getters are must for every class.So, whay doesn't JdbcProductDao doesn't have any?

Thanks,
[ October 04, 2008: Message edited by: K Aditi ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look in the SimpleJdbcDaoSupport class that you are extending.

Now, I will point out this, I would not extend that class. At the point where you go "extends SimpleJdbcDaoSupport" you are now tightly coupled with a Spring class.

The best way to do JDBC Repositories is to inject a SimpleJdbcTemplate into your Repository, and use the template. In your xml you will declare a SimpleJdbcTemplate to inject into your JdbcProductDao, which would require you to create a private instance variable to hold it, and write a setter method to inject it.

Mark
 
K Aditi
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply! But isn't constructor rather than a setter method preferred in this scenario?
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by K Aditi:
Thanks for the reply! But isn't constructor rather than a setter method preferred in this scenario?



Not if you put @Required on the setter method.

And actually if you use @Resource on the instance var, you don't need it in the constructor or a setter.

Mark
 
K Aditi
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Currently, I am not using autowiring and annotations...I am configuring it all manually...just to get of it.

Thanks,
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic