my dog learned polymorphism*
The moose likes Other Application Frameworks and the fly likes Spring: Configuring bean property 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: Configuring bean property " Watch "Spring: Configuring bean property " New topic
Author

Spring: Configuring bean property

K Aditi
Ranch Hand

Joined: Mar 17, 2008
Posts: 89
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 ]

Aditi
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
K Aditi
Ranch Hand

Joined: Mar 17, 2008
Posts: 89
Thanks for the reply! But isn't constructor rather than a setter method preferred in this scenario?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

Joined: Mar 17, 2008
Posts: 89
Currently, I am not using autowiring and annotations...I am configuring it all manually...just to get of it.

Thanks,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring: Configuring bean property