GreenTea Profile Series - Service Layer

2009-09-09  来源:本站原创  分类:Java  人气:241 

Because the view layer (client) the contents of the large and relatively trivial, so let's introduce the service layer design. The basic idea of service layer is dependent on programmable injection method.

Overview

Now in service levels prevalent use of Spring, a very good way dependency injection. GreenTea not replace or negate the Spring design (frankly, have no such capability), just want to make development easier. Spring's a good idea, but in the framework of the use of SSH was a bit excessive, is the so-called "hands are the hammer and see everything is a nail." In general, the completion of a common feature, Spring needs an interface, an implementation class and configuration file of a record (for a long time do not use Spring, and now is still the case?). This reflects the separation of interface and achieve the realization of good, but this approach really play a role in the practice begin? In my project done, I found that 95% of the implementation class does not change before, in other words, 95% of the interface is not playing its due role, but rather increased the development of a drag.

-Oriented programming interface, the idea is no problem, but the need to consider whether he can play a practical effect. Engineering technology is a trade-off is the pros and cons of the trade-offs, there is no perfect, only the optimal solution. Spring initial idea is to simplify the development of EJB, but I feel not enough to simplify. Therefore, I used this place in the service layer of "programmatic" approach to complete most of the dependency injection problem. Of course, you can also use this level of Spring, but is GreenTea's Service Layer degenerated into Struts (old version) of the Action only, but also something that has not.

Principle

Gossipy few Syria to introduce the idea of GreenTea for service layer to achieve very simple class diagram is as follows:

GreenTea Profile Series - Service Layer

ProcessorFactory is a processor factory, its role is to generate or create a new Processor instance (of course, you can use the object pool). Note that: in the application throughout the life cycle is only one instance, it is not thread-safe in use need attention. An application may require multiple processors, factories, can be added directly in the configuration file.

Processor is really to work, class, responsible to the Service into their required information. Different Service may require different information, so need to be able Processor Identification Service (eg: Service need to implement a specific interface). Processor methods include a Serivce method calls throughout the life cycle, thus providing resource information to the Service's full control.

Service do not need to say, that the business logic.

Achieve

To put it slightly above the abstract, give a concrete realization of it would be very clear understanding of its structure. The JPA implementation in order to achieve the injection, for example, for reference, if you need to use at work, need to adjust and robust.

First look at processor factory class.

public class JPAProcessorFactory implements ProcessorFactory (

private EntityManagerFactory factory;

public void onInit () (

/ / First, create a persistent chemical class (multi-line configuration code is omitted here)

factory = Persistence.createEntityManagerFactory ( "emName", properties);

)

public synchronized Processor createProcessor (Class serviceClass) (

/ / If the service class need to use JPA for persistence processing, then to create a persistent processor.

if (JPAAware.class.isAssignableFrom (serviceClass)) (

EntityManager em = factory.createEntityManager ();

JPAProcessor processor = new JPAProcessor (em);

return processor;

)

return null;

)

public void onDestroy () (

/ / Close the plant

factory.close ();

)

)

Take another look at "Processor", is also very simple message for him to complete the injection has been the method call when the life cycle of all of this.

public class JPAProcessor implements Processor (

private final EntityManager entityMananger;

public JPAProcessor (EntityManager entityMananger) (

this.entityMananger = entityMananger;

)

public void onCreate (Class serviceClass, Session session, Context context) (

)

public Object [] onBefore (Object service, Method method, Object [] params) (

/ / Before the method call to start a thing, and assigned to service class

entityMananger.getTransaction (). begin ();

((JPAAware) service). SetEntityManager (entityMananger);

return params;

)

public void onAfter (Object service, Object ret) (

/ / Method to commit the transaction on the successful conclusion of

entityMananger.getTransaction (). commit ();

)

public void onFinally () (

)

public void onThrowable (Throwable t) (

/ / Roll back the transaction out of the problem

entityMananger.getTransaction (). rollback ();

)

)

Followed by "JPAAware", whose main class is not used to identify the need to use JPA for persistence, and inject the necessary "entity manager."

public interface JPAAware (

public void setEntityManager (EntityManager entityManager);

public EntityManager getEntityManager (entityManager);

)

Finally, we come back implementation class.

public class UserService implements IService, JPAAware (

private EntityManager entityManager;

public void register (User user) (

/ / Direct access "is injected into" entity manager

entityManager.merge (user);

)

public void setEntityManager (EntityManager entityManager) (

this.entityManager = entityManager;

)

public EntityManager getEntityManager () (

return entityManager;

)

)

As can be seen from the above code, in the preparation of business logic, the programmer does not care about how to get entity manager, how to control the affairs, how to close the plant, these things are "Processor" reunification deal with.

In practice, the use of which can be the most common interface and the basic realization of the abstract to a AbstractService inside, so the code can do amazing simplicity. If we want to use Spring, is fully capable of achieving a Spring Zhaomaohuahu the Processor.

Features

  1. To develop simple, although the development of "Processor" a little bit more content, but only early in the project used in the development of business logic should be very simple and straightforward.
  2. No configuration, reducing unnecessary coupling point.
  3. Get rid of dependence on the container, so that the development and unit testing easier. In addition, the lifting of the coupling between this layer, more conducive to reuse of services.

The expansion of services

If we do need an interface to the project can be replaced at any time, GreenTea also provided in two ways: Configuration and conventions.

Configuration, by definition, GreenTea will be in the configuration file to find the relationship between the control interface and implementation. Add the following code in the configuration can be:

<param name="serviceMapping" type="map">
<entry key="com.xx.yy.AService" value="com.xx.yy.AAService" />
</ param>

Note: AService can also be a concrete class, namely: not limited to the relationship between interface and implementation, but also can be a parent class and subclass relationships.

Convention is by implication, a way to determine the relationship of this control. For example: com.xx.yy.AService is an interface or abstract class, the framework will try to find "com.xx.yy.AServiceImpl" as its implementation class.

Have achieved

The system has provided several basic implementation, for reference.

Name

Description

IPOPersistenceProcessor

GreenTea comes with a simplified version of the O / R mapping mechanism.

InjectorProcessor

When you need service call another service, it is best to use this service into the way to ensure that call the method within a transaction.

SessionProcessor

Session service provides relevant information.

FileServiceProcessor

For service providers a unified document processing interfaces. I am working in an often encountered problem is that some programmers forget to turn off the use of document, causing the memory leak, resulting in system instability. Use this interface to a certain extent, to reduce such problems.

Conclusion

All for the sake simplicity, reduce the maximum coupling point. Musical greetings cards, what would be the consequences?

  1. While the emphasis is more spent some effort, but the knife and the use of chicken the effect is the same;
  2. Although chickens killed, but hurt his hand;
  3. Although chickens killed, but more cut off a leg;
  4. You brandish a heavy chopper from the waist when loose, but also allow the chicken to run.

相关文章
  • GreenTea Profile Series - Service Layer 2009-09-09

    Because the view layer (client) the contents of the large and relatively trivial, so let's introduce the service layer design. The basic idea of service layer is dependent on programmable injection method. Overview Now in service levels prevalent use

  • GreenTea Profile Series - Hello World 2008-10-12

    An overview of the previous release a little bit jumbled, less intuitive, so this time to a few sentimental bar. Or old habits, the first to write a "Hello World". The completion of these tasks only need to prepare two documents, without any con

  • DAO layer, Service layer, Controller layer. View layer 2010-02-09

    http://wwwlike.javaeye.com/blog/570991 DAO layer: DAO layer is primarily for data persistence layer, is responsible for liaison with the database, some tasks are encapsulated in This , DAO layer design is the design of the first DAO's interface, and

  • JAMES V3 profile series spring-server.xml 2011-09-18

    AliKevin wrote Apache James 3.x after using the Spring framework, configuration file name and organization with great changes have taken place 2.x, 3.x version of this series will describe the main configuration file. This paper introduces the Spring

  • Spring + Hibernate transaction control on the Service Layer Processing 2010-04-23

    <!-- DataSource,SessionFactory start--> <bean destroy-method="close"> <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> <property name="url" value="jdbc:hsqldb:hsql://localh

  • the method in the implementation of the order and the servlet API profile (init, service, doservice, doget, etc.) 2010-05-28

    These days want to EclipseBirt of web publishing package for some of the features of the expansion, inevitably will need to modify the viewservlet the source, in order to change the first read must be straightened out, he spent one day shocked the wh

  • Provided with Spring DAO JUnit framework for expansion of the unit test or Service Layer 2010-06-10

    Behind unit testing and testability of components is through the application of the core concepts of Spring. Has been independent of the Java object and test than, as J2EE Web components must run in a container, or even HTTP-based Web environment int

  • Spring ssh2 framework layer in the SERVICE configuration Affairs 2010-06-04

    Version: spring2.5.5 + hibernate3.3.1 + struts2.0.11.2 + database postgresql Spring configuration services layer in the SERVICE applicationContext.xml file contents: <? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = &

  • On the Web development framework of a new idea --- GreenTea 2009-12-05

    Has done a 9-year program development, to be regarded as an old programmer domestic standards, and is still passionate about the technology. While this idea is not popular in the country is still in the persistent adherence. In these years, the devel

  • JavaEE of Service 2009-05-01

    1. Information "Java Transaction Design Strategies" InfoQ minibook, the best electronic documents, Java in a silly state of affairs must-read. "Expert One on one J2EE Development Without EJB English version" and the Spring reference ma

  • AppFuse Study Notes - Model Layer 2009-07-28

    3.3 Model structure Model layer is the core of the entire system, complete the application business logic and database communication. Model will be divided into two AppFuse: persistence layer and business layer. Spring + Hibernate framework used to a

  • SSH package four: Hibernate.cfg.xml from Spring management, use services, services in the DAO layer 2009-08-02

    In the SSH package to amend the basis of three to four SSH package SSH package four: Hibernate.cfg.xml from Spring management, use services, services in the DAO layer *** Note: SSH package combination of the four programs in the SSH add a layer of th

  • hibernate Study Notes 3 - Service Management 2009-09-24

    Hibernate recent study on design pattern dao have some doubts, summed up the program the following affairs: 1, involves only a simple business process The use of JDBC services can hibernate at this time do not need to service layer, as long as enough

  • Spring Service 2010-03-29

    Database transaction four characteristics: Atomic (atomic): have been successful or have failed; Consistency (consistency): transaction operation, the database in which the state and business rules are the same; such as a, b account for each transfer

  • service development specification (change) 2010-04-25

    1: Use assert control input. Use assertions to determine the effective input, so to avoid the proliferation of abnormal, rapid positioning errors and reduce the chances of BUG appear. Such as: Java code 1.import org.springframework.util.Assert; impor

  • Subject: How do I write Service of the zz 2010-03-23

    Subject: How do I write Service of the zz since: http://www.javaeye.com/topic/606348 <script type="text/javascript"> </ script> soon to bid farewell to research and development, so to write some of his own experience, is used to mark

  • How do I write Service of the [transfer] 2010-03-23

    Original: http://www.javaeye.com/topic/606348 Soon to bid farewell to research and development, so to write some of his own experience, is used to mark the four years, Buy & Sell Forum, that this is the first used to introduce himself how to write Se

  • The services layer on the rails 2010-08-27

    ROR has been limited because of the MVC's three-tier structure, so that service levels will be lost, when doing the project would bar service layer business logic written in the controller layer of the action, from the logic seems confused, if the se

  • android system comes with the Service and use of the principle 2010-09-29

    1. Description android's background in many service, they are SystemServer open system startup, normal operation of support systems, such as SD card MountService monitor whether the installation and removal, ClipboardService provide clipboard functio

  • environment configuration spring mvc Series 1 2010-10-02

    Nonsense: Hot? = Spring + hibernate + struts Hot? = Spring + jpa + struts Hot? = Spring + ibate + struts Personally feel that sub-fan complex configuration, began to turn spring, because spring is said to have their own set of things, and thus be to