Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which classes should I turn to beans in a Spring project? [closed]

Tags:

java

spring

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?

like image 997
polis Avatar asked Jun 25 '14 06:06

polis


1 Answers

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.

like image 81
Serge Ballesta Avatar answered Nov 14 '22 22:11

Serge Ballesta