AppFUSE learning notes - model layer

xiaoxiao2021-03-06  25

3.3 Model layer structure The Model layer is the core part of the entire system, completes the application's business logic and communication with the database. Model is divided into two layers: persistence layer and business layer. Using the Spring Hibernate framework, here is described in detail with the operation of the user user data as an example.

Access to persistence data is implemented based on the DAO (Data Access Object) mode. The DAO mode provides an interface to all interface operations required to access the relational database system. The DAO mode offers the underlying data access operation with the high-level business logic, providing an object-oriented data access interface to the upper layer.

The Model layer is related to the user: Pojo: User: The business object of the administrator table. Business Floor: UserManager: The business layer interface is called for the control layer. UserManagerImpl: The implementation of the business layer interface, calls the persistence layer interface. Persistent layer: Userdao: The persistent layer interface is called for the business layer. Userdaohibernate: Realization of the persistent layer interface. XML configuration file: ApplicationContext-Service.xml: The configuration file of the business layer interface. ApplicationContext-hibernate.xml: Profile of the persistent layer interface.

3.3.2 Spring's IOC IOC (Inversion of Control) is reversed. The IOC mode is that the Dependency Injection mode is the meaning of relying on injection, that is, the relying on the first peeling, and then injecting injecting. Spring's lightweight Bean container provides IOC type assembly capabilities for business objects, DAO objects, and resources (such as JDBC Data Sources, or Hibernate SessionFactorie et al.). Spring uses an XML format application profile to manually manually manage the selectivity of single instance objects or factory objects by parsing custom property files. Since Spring makes non-intrusionability as an important goal, it is possible to complete the configuration of Spring's own interfaces and classes can be done by Spring configuration management. In terms of implementation, Spring takes the form of the configuration file to implement the injective injection, and supports Type2 IOC (SETTER INJECTION) and Type3 IOC (Constructor INJECTION). In the Model layer, use SETER INJECTION (TYPE2) injects provided by Spring. Take User as an example, below is the usage. Defined in the applicationContext- hibernate.xml There is a sentence in the UserManager class: public void setUserdao (Userdao DAO); this is a DAO Object setting method (syringe). Userdao will be called, and persistent layer communications. Create an instance of Userdao in this way, and the purpose of creating Userdao by UserManager. Avoid direct implementation of the implementation of the UserDao to close the business layer and the persistence layer.

When the traffic layer method is called in the control layer, use the service locator to return to Spring Context, Spring BeanFactory provides a getBean method. Beanfactory is a universal Factory that enables objects to be obtained by name, and can manage the relationship between objects. Configuring in the applicationContext-service.xml Define Universal Method in Control Layer BaseAction: Private Static ApplicationContext CTX = NULL; PUBLIC Object GetBean (String Name) {if (ctx == null) {ctx = WebApplicationContextUtils .getRequiredWebApplicationContext (servlet.getServletContext ());} return ctx.getBean (name);} UserManager instance created in the UserAction: UserManager mgr = (UserManager) getBean ( "userManager "); This way, through BeanFactory's getBean method, and XML configuration files, avoid direct instantiation of UserManager in the UserAction class, eliminating coupling between the control layer and the business layer and the business layer and the persistence layer, realization of the dependent injection . ApplicationContext is the sub-interface of BeanFactory, providing support for the following things: information search, supporting international event mechanisms, allowing publishing objects and optional registration to receive event portable files and resource access

3.3.3 Spring transaction management Outstakes in data persistence, may be the most shining advantage of Spring. Spring provides external management of transactions through the intensive parametric transaction mechanism of the container. The parametric transaction of the container management provides considerable flexibility for program development, and because of the management of the transaction to the container, the application logic does not need to write the transaction code, which greatly saves the amount of code (especially for multiple transactions, more transactions) The application of resources increases productivity. AppFuse in applicationContext-service.xml configuration file of the transaction < ref bean = "transactionManager" /> PROPAGATION_REQUIRED PROPAGATION_REQUIRED Propagation_required, readonly defines a TransactionProxyFactoryBean service called TXProxyTemplate. It performs a transaction package for a persistent layer object containing actual data logic. Here, through the TransactionAttributes property, we specify the management policy of the transaction, and all the names begin with Save and Remove are incorporated into the transaction management. If you throw an exception in this method, Spring rolls the current transaction, if the method is ended normally, the transaction is submitted. For all other methods, the only transaction processing mechanism is processed. Set to a read-only transaction, you can optimize the data operation, such as the only transaction Hibernate will not perform FLUSH operation, and some database connection pools and JDBC drivers are also specially optimized for read-only operations. If there are other methods that need to write database operations, you can declare in the appropriate Manager configuration.

As UserManager in, add the attribute PROPAGATION_REQUIRED PROPAGATION_REQUIRED propagation_required propagation_required, readonly The method ending with LogIncookie can also write a database. Spring can incorporate any Java Class into transaction management without any modification, so our classes may not know that it is being managed. 3.3.3 Spring Hibernate Operation Springs Spring has good support for Hibernate. Create and maintain session through SessionFactory in Hibernate. Spring integrates the configuration of the sessionFactory without setting the sessionFactory via hibernate.cfg.xml. The MappingResources property of the SessionFactory node contains the path to the mapping file. Multiple mapping files can be configured under the List node. The HibernateProperties node accommodates all property configurations. The SESSIONFACTORY configuration here can be interpreted corresponding to the traditional hibernate.cfg.xml file structure. The following is HibernateSessionFactory HibernateTransactionManager and arranged: in the applicationContext-hibernate.xml: com / mycompany / model / user.hbm.xml ................................................. List>

@ hibernate-dialact @ True Update Spring provides a HibernateTransactionManager, using Hibernate-oriented TransactionManager implementation: Org.SpringFramework .hibernate.hibernateTransactionManager. He bundled a Hibernate Session with a thread to support Transactions.

The SessionFactory Bean references HibernateSessionFactory, while the TransactionManager Bean references HibernateTransactionManage. There is a sessionFactory property in the TransactionManager Bean. HibernateTransactionManager has a sessionFactory Setter and getter method to implement "Dependency Injection" when Spring starts. Quote SessionFactory Bean in the SessionFactory property. These two objects were assembled after the Spring container was initialized. User uses a TransactionProxyFactoryBean, which defines a setTransactionManager (). It is also very convenient to deal with the declared things and service object. TransactionProxyFactoryBean has a setter. This will be referenced by Business Service Object (UserManager), and UserManager defines the business layer, and it has an attribute, referenced by setUserDao ().

转载请注明原文地址:https://www.9cbs.com/read-44515.html

New Post(0)