I'm learning a spring framework and I have a question. When I create a Java/Spring project, which classes should I choose to describe as beans in Spring xml? For example: if I want to access DB table called Student, I create these classes: Student
, StudentMapper
(implements RowMapper) and StudentJDBCTemplate
(DAO). Should I describe every these classes in my project as Spring Beans? What are the criterias for choosing class to describe it as Spring Bean?
The rule is if an object has a reference to another object, then both should be beans to have the second injected in the first. I suppose you will have a DAO and a service. Both must be beans and do not forget that an injected bean should allways be declared through an interface. This rule is not absolute but it will allow you to easyly use AOP if you need to, and is anyway a good practice in java programming.
In contrast, Student
has normally no reason to be a bean (should not be injected nor have anything injected into), and the RowMapper
implementation can simply be an inner class of the Dao because it should never be used elsewhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With