1

I am using spring 3.0 with annotation which i am creating dao interface , then dao implementation class, then creating service interface and then creating service implementation class.

and using object of service implementation into controller. this process is too lengthy.

can i use only dao class and directly use it in controller?

@service
@controller
public class MyController { ... }
Pierre Henry
  • 16,658
  • 22
  • 85
  • 105
swapnil shahane
  • 243
  • 1
  • 4
  • 13

2 Answers2

2

You can but you really really really shouldn't. If you have any plans of putting this code into production, then you need to keep your layers well defined. This is because each layer has a different responsibility.

Your DAO layer is responsible for all database access. It doesn't care what object you want, what you want to do with it, and when you want it done. It only cares about how it's done.

Your Service layer is responsible for what you want to do to your objects. It contains all of your business logic, convenience methods and may use multiple DAO's inside a single service. Your service layer is where you handle your database transactions as well.

Your controller layer is responsible for how you want to show your data to the user. It only contains services and minimal logic concerned with how to display the data returned by your service layer.

JamesENL
  • 6,400
  • 6
  • 39
  • 64
  • ok, then can i skip creating interface and directly create class only? – swapnil shahane Apr 15 '14 at 09:05
  • If you skip creating interfaces, then Spring has nothing to autowire into your classes. Spring takes your interfaces and makes proxy objects that it injects when you Autowire objects. So yes, you can skip creating interfaces if you never want to autowire these services and DAO's into other classes. (Big Hint: CREATE INTERFACES) – JamesENL Apr 15 '14 at 09:09
  • ok, but i have seen an example on web in which programmer directly use dao in controller and didnt create interface. so which version user had used or it was wrong code. [link](http://www.javacodegeeks.com/2011/04/spring-mvc3-hibernate-crud-sample.html) – swapnil shahane Apr 15 '14 at 09:14
  • Yes, that is an **example** of how to write a DAO. That is not production code. That should never go anywhere near a production environment without a service layer in between. You would be violating one of the fundamental design patterns of Spring to do this. As examples go, that is not one that I would be using to learn how to make DAO's. All your transaction management should be in your service layer. That means all your @Transactional annotations go on methods in your service classes. – JamesENL Apr 15 '14 at 09:21
2

I think there are 2 distinct questions being asked here :

Is it really necessary to have all three layers : web layer, service layer, and DAO ?

and :

Do Spring beans necessarily need to implement and interface and to bve (auto)wired by interface rather thant concrete class ?

The answer to both is the same : it is not a necessity, but it is strongly recommended for anything other thant trivial projects.

For the first question, yes it is better so separate concerns in different layers. However I must admint that my controllers sometimes access the DAOs directly for simple read-only tasks in some projects. Sometimes the service layer seems like too much when all it does is to map the DAO methods. However I always use a service layer for transactional business logic that actually modifies the data.

For the second question : Spring can, without problem, instantiate and inject beans that don't implement any interface. However you will start to have problems if you use more advanced stuff linked to aspect-oriented programming, the most common being the @Transactional annotation. In this case Spring has to create proxies to your objects, and it is all simpler to create proxies of interfaces. Ohterwise it has to manipulate bytecode and it introduces limitations.

Perhaps more importantly, "programming to an interface" is a good practice regardless if you are using Spring or not, or even Java or anothrer language. A simple google search will bring up many good articles on this, or SO questions such as this one.

So again, while you can live well without both things in the short term, both will make your life much better in the long term.

Community
  • 1
  • 1
Pierre Henry
  • 16,658
  • 22
  • 85
  • 105