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
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
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
As UserManager in, add the attribute
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 ().