Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring: Configuring bean property

 
K Aditi
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • 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 ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic