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).
public interface CustomerService {
void placeOrder(Order order);
Set getOrders();
void add(Customer customer);
Customer get(long id);
}
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) {
customerDao.persist(customer);
}
}
Transactions
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. |
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?
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.
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