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.
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.
Gossipy few Syria to introduce the idea of GreenTea for service layer to achieve very simple class diagram is as follows:
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.
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);
public void onDestroy () (
/ / Close the plant
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);
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
public void setEntityManager (EntityManager entityManager) (
this.entityManager = entityManager;
public EntityManager getEntityManager () (
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.
- 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.
- No configuration, reducing unnecessary coupling point.
- 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" />
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.
The system has provided several basic implementation, for reference.
|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.|
All for the sake simplicity, reduce the maximum coupling point. Musical greetings cards, what would be the consequences?
- While the emphasis is more spent some effort, but the knife and the use of chicken the effect is the same;
- Although chickens killed, but hurt his hand;
- Although chickens killed, but more cut off a leg;
- You brandish a heavy chopper from the waist when loose, but also allow the chicken to run.