Jul 20th, 2010, 04:23 AM
Should everything be a spring bean?
I still quite new to spring and I understand the concept of IoC/DI. What I can't quite understand (as I haven't had the exposure to spring in a real environment yet) is to when spring beans should or more to the point should not be used.
My understanding is that you should used interfaces and implementations and allow spring to create instances of the required implementations.
Take this example. In a web application I want to query a repository of some sort to get back a list of personnel records. Each record has: name, job title, location.
I can quite clearly see that the repository would be best created with an interface and then have an underlying jdbc implementation which is accessed as a spring bean. This would then access a datasource spring bean. This makes perfect sense to me.
What about the personnel records. My initial thought would be to not use an interface and just have a javabean with the required member variables and map the data from the repository into the member variables (through setters) creating a new instance for every personnel record. This is indeed how the example in the reference manual does it
This goes against my understanding of Spring's main purpose of decoupling the various objects in the application from each other.
Could anyone enlighten me please.
Jul 20th, 2010, 07:04 AM
You should use spring to wire your applications beans together (services, daos, infrastructure etc). You really don't want to express your data structure (Person -> Company etc) in spring.
Jul 20th, 2010, 07:16 AM
I see. That makes sense. Thank you.