Using Open Source Product Assembly Your Web Application Schema (2)
A simple example
Since we have already understood these components from globally. Let us start practice now. We still use struts, Spring and Hibernate. These three frameworks have been described enough, and they will not be repeated here. This article example guides you how to use these three frameworks to integrate development and reveal how a request runs through each layer. (Add an ORDER to the database from the user, display; and then update, delete).
From here you can download to the program program original code (Download)
Since each layer is interacting, we will first create Domain Objects. First, we must determine those of these objects to be persistent and which are provided to Business Logic, those are the design of the display interface. Next, we will configure our persistence layer and define Hibernate or mappings. Then define business objects. With these components, we will connect them with Spring. Finally, we offer Spring a persistent layer, from this persistence we can know how it communicates with the Business Service Layer, and how it handles other layers thrown. .
Domain Object Layer
This layer is encoded, our coding begins with this layer. In the example, ORDER and OrderItem are one one-to-many relationship. Here is two objects of Domain Object Layer:
· Com.meagle.bo.order.java: Contains a summary information of an Order
· Com.meagle.bo.orderLineItem.java: Contains details of ORDER
Consider how your package is named, this reaction is how you are layered. For example, Domain Objects may be packaged in com.meagle.bo in the program. A more detailed one will be packaged below the sub-directory of com. Meagle.bo. Business logic should be packaged from com.meagle.Serice, while the DAO object should be in com.meagle.service.dao.hibernate. The PRESentation Classes of Forms and Actions should be placed separately in com.meagle.action and com.meagle.forms. Accurate giving your package makes your Classes very segmented and easy to maintain, and when you add new classes, you can keep up and down on the program structure.
Persistence Layer Configuration
It takes several steps to establish a persistence of Hibernate. The first step allows us to persist BO. Since Hibernate is working through Pojo, the ORDER and ORDERLINEITEM objects need to add Getter, setter methods to all Files. Hibernate mapping (OR) objects via an XML file, the following two XML files map ORDER and ORDERITEM objects, respectively. (Here is a XDoclet tool to automatically generate your XML image)
ORDER.HBM.XML
ORDERLINEITEM.HBM.XML
You can find these XML files in a webcontent / web-inf / class / com / meagle / bo directory. Hibernate's sessionFactory is used to tell the procedure to communicate with which database communication, which connection pool or uses DataSource, which persistent objects should be loaded. The Session interface is used to complete these operations for Selecting, Saving, Delete, and Updating. Behind us will explain how SessionFactory and Session are set. Business Layer Configuration
Since we already have Domain Objects, we will want business service objects, use them to execute the program's logic, call the persistence layer, get the requests of the UI layer, process Transactions, and control Exceptions. In order to connect these and easy to manage, we will use aspective SpringFramework. Spring provides control inversion (Inversion of Control 0 == IOC) and SETTER Dependency Injection (optional), connects objects with an XML file. IOC is a simple concept (it allows an object to be created in the upper layer), using IOC to release your object from the creation, reducing coupling.
Here is an example of an object that does not use IOC, which has a high coupling.
Figure 2. No IOC. A creates B and C
Here is an example of using IOC, which allows objects to be created and entered on the high layer, so this can be directly executed.
Figure 3. Objects use IOC. A contains the setter method of accepting B, C, which also achieves the purpose of creating B, C by A.
Establish our business service object (Building Our Business Service Objects)
The setter method in Business Object accepts the interface so that we can define object implementations very loose, and then inject. In our case, we will use a Business Service Object to receive a DAO, use it to control the persistence of Domain Objects. Since Hibernate is used in this example, we can easily use other persistent frameworks to notify Spring Spring has new DAOs.
In the interface-oriented programming, you will understand how the "Injection Dependence" mode is loosely coupled to your business logic and persistent mechanism :).
Here is an interface business service object, DAO code snippet:
Public interface iorderservice {
Public Abstract Order SaveneWorder (Order Order): PUBLIC ABSTRACT ORDER SAVENEWORDER
Throws OrdeRexception,
ORDERMINIMUMAMOUNTEXCEPTION;
Public Abstract List FindorderByuser
String User
Throws Orderexception;
Public Abstract Order FindorderbyId (INT ID)
Throws Orderexception;
Public Abstract void setorderdao
Iorderdao OrderDao);
}
Note that there is a setOrDao () in this code, it is a DAO Object setting method (syringe). But there is no way to getORDAO, this is not necessary, because you will not access this ORDERDAO outside. This Dao ObjectE will be called, and communicate with our Persistence Layer. We will use Spring to match Dao Object and Business Service Object. Because we are interface-oriented, it is not necessary to couple the category coupled together. Next, we start our DAO implementation class for encoding. Since Spring has supported Hibernate, this example directly inherits the Hibernatedaosupport class, this class is useful, we can refer to HibernateTemplate (it is mainly a use of HibernatedAosupport, the translation: You can view the Srping API). Here is this DAO interface code:
Public interface iorderdao {
Public Abstract Order Findorderbyid
Final Int ID);
Public Abstract List FindordersplaceByuser
Final String PlacesDBY;
Public Abstract ORDER SaveOrder
Final ORDER ORDER;
}
We still have to assemble a lot of associated objects to our persistent layer, which contains HibernateSessionFactory and TransactionManager. Spring provides a HibernateTransactionManager, which is bundled with a hibernate session using it to support Transactions (see threadlocal).