views:

55

answers:

2

When developing Java EE applications how do I separate Business Logic so it can be reused?

I inherited an application that is mostly Model 1. Business logic is located in JSPs, Servlets and DAO code.

I want to separate the business logic but I am confused by all of the frameworks etc. that exist.

I am looking into Hibernate with JPA to handle all database persistence. Currently all SQL is hand coded and separate SQL is used for different RDBMS. My DAOs will call the code necessary for persistence.

I am thinking of using Struts for my web layer. The part I don't understand is the Business Logic.

I don't want my logic tied to the Web Layer because I want to reuse the logic in a Java SE application.

I thought about putting business logic in Entity classes but that seems like a bad idea.

Is there some technology or pattern that can be used as a guideline for creating reusable business logic?

If I am not clear I will edit.

Thank you.

+2  A: 

To separate your frontend code (the view) from your business logic (controller) and your data (model) you can follow the MVC pattern.

You can have your controllers access other classes that contain the reusable business logic that will be used within your Java SE applications.

There are a lot of frameworks that help you to build web applications in this style like Grails (uses Groovy), Play or Roo. But because you said 'enterprise' you should have a look at the Spring framework and its MVC module. Spring offers good integration with Hibernate and allows you to follow the MVC pattern with your web applications.

moxn
@JimFerrans Thanks for spellchecking
moxn
@moxn: I hope one day to become a copy editor at O'Reilly! ;-)
Jim Ferrans
A: 

I would say take it piece-meal. Solve the biggest problems first, which is in your case having business logic in the jsp pages. You can accomplish this using any web MVC framework of your choice (Struts, Spring MVC, Grails are all good. Pick one that you are most comfortable with).

The next problem is organizing your business logic in a separate Model layer that your controllers can invoke. Spring is a good DI framework for organizing and bootstrapping your application. Also, Spring supports a number of web MVC frameworks including Struts, JSF etc.

The last problem is your Dao layer. You mentioned you want to use Hibernate/JPA. I dont know how familiar you are with Hibernate, but make sure that you are trying to solve an actual problem by switching to Hibernate (since switching to Hibernate usually comes at a significant cost and headaches).

Sasi