swapnil shahane
swapnil shahane

Reputation: 253

how to use spring 3.0 without service?

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 { ... }

Upvotes: 1

Views: 191

Answers (2)

Pierre Henry
Pierre Henry

Reputation: 17487

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.

Upvotes: 2

JamesENL
JamesENL

Reputation: 6540

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.

Upvotes: 2

Related Questions