Dashboard > JJGuidelines > Home > Chapter 3 - Design > 3.2.2. Service layer
JJGuidelines Log In | Sign Up   View a printable version of the current page.
3.2.2. Service layer
Added by Stephan Janssen, last edited by Stephan Janssen on Mar 08, 2007  (view change)

3.2.2. Service/Business Layer

This service/business layer is really the heart of your project.

The service components are called by either the presentation layer or triggered directly through web services calls. In addition the persistence DAO objects get injected where needed within the service components. In many cases we'll also start the transaction on the service method and propagate it to the DAO component(s), with the Spring framework this is just a matter of configuration actually using AOP behind the covers.

Often in this layer we've opportunities for introducing work-flow engines (BPEL based) and/or move our business logic to rule engines (possibly through the JSR-094 interface).

A common mistake is introducing business logic in the Struts action classes of the presentation layer or in the DAO objects. You should try to centralize presentation logic (like GUI validation) and persistence logic in the related layers. However this guideline will become less black & white once you start using session beans as backing beans for your JSF components using for example the Seam framework and eventually Web Beans.

The advantage of moving all business logic in the service layer is that different presentation strategies can reuse your logic. Also the additional web services support can be almost transparently introduced using the pluggable remoting strategy of the Spring framework.

Service Components

In 'helloworld' kind of examples the service component is nothing more than a pass through invocation to the DAO. However in more larger projects the service component can be a composition of other finer grained (basic) services either locally or remotely available and of course lots of additional business logic.

Below you can find a CustomerService example (javadoc is omitted to improve clarity of example).

CustomerService.java
public interface CustomerService {

   void placeOrder(Order order);

   Set getOrders();

   void add(Customer customer);

   Customer get(long id);

   //...
}
CustomerServiceImpl.java
public CustomerServiceImpl implements CustomerService {

   private CustomerDao customerDao;

   /**
    * The CustomerDao gets injected through Spring. 
    */
   public void setCustomerDao(CustomerDao customerDao) {
      this.customerDao = customerDao;
   }   

   void add(Customer customer) {
      
      // other business logic here (for example: is customer black listed ?)

      customerDao.persist(customer);

      // other business logic (for example: send email notification etc.)
   }

   //...
}

Transactions

I encourage you first to read Chapter 9. Transaction Management from the online Spring Framework documentation.

todo: Example

DAO injection

todo

Work flow

todo

Rule Engines

todo

Web Services

The Spring framework supports plug-able POJO remoting strategies. Out-of-the-box Spring supports RMI, JAX-RPC, Hessian/Burlap, JMS and HTTPInvocation. However you can also write your own handlers or use external developed handlers like XFire.
Parleys.com hosts some very interesting related Spring talks on JMS by Juergen Hoeller and Spring Web Services by Arjen Poutsma.

Presentation + Service layer = Web Beans

With the introduction of Seam and eventually Java Web Beans (JSR-299) the artificial gap between the presentation and service layer will fade out.

todo

I think there is a fundamental drawback when using remoting for exposing services to other applications. I have been blogging about this topic some years ago : Should you use remoting for a SOA?
I do think that it is a best practice to have a mediation layer between the service interface and the façade exposed by the application. This mediation layer should support XML to Java customizable mappings enabling the support of many service interface versions with a single implementation of the service.
If you really look at Spring Web services, it is not related at all to Remoting. Spring Web services aims to facilitate contract-first SOAP service development, allowing for the creation of flexible web services using one of the many ways to manipulate XML payloads. Use Spring-WS for exposing services, don't use remoting.

Posted by Robin Mulkers at Mar 09, 2007 02:23 | Permalink

Agreed. This is why the service layer (and others) are all still in working draft. However feel free to join in on this topic Nevertheless using the Spring remoting does allow you to introduce JAX-RPC and XFire from a bottom-up (code first and I know many prefer the top-down or schema first) approach enabling you to active your services through theses API's in a very easy way...

Posted by Stephan Janssen at Mar 10, 2007 03:59 | Permalink
Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Hosted by JavaLobby
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.5 Build:#520 Jun 27, 2006) - Bug/feature request - Contact Administrators