Struts,hibernate,spring

Source: Internet
Author: User
Tags to domain

  • How does hibernate work and why should I use it?
    • Principle:
      • Read and parse the configuration file
      • Read and parse mapping information, create Sessionfactory
      • Open Sesssion
      • Create Transaction Transation
      • Persistent operations
      • Commit a transaction
      • Close session
      • Close Sesstionfactory
    • Hibernate is used for the following reasons:
      • 1 The code for JDBC access to the database is encapsulated, which greatly simplifies the tedious repetitive code of the data access layer.
      • Hibernate is a JDBC-based, mainstream persistence framework that is an excellent ORM implementation. He simplifies the coding of the DAO layer to a great extent
      • Hibernate uses the Java reflection mechanism rather than the bytecode enhancer to achieve transparency.
      • Hibernate has a very good performance because it is a lightweight framework. The flexibility of the mapping is excellent. It supports a variety of relational databases, from one-to-one to many-to-many complex relationships.

  • How does hibernate delay loading?
    • Entity Object Lazy Loading
    • Collection (Collection) Lazy loading
    • Property Lazy Loading
    • When hibernate is querying the data, the data does not exist in memory, and when the program actually operates on the data, the object exists in memory, and the delay is loaded, which saves the server's memory overhead and improves the performance of the server.

  • hibernate? ( such as: one-to-many, many-to-many relationships)

      &NBSP in the configuration file;

    • hibernate, The cache has a first-level cache and a two-level cache concept, a primary cache is required, in the session section, the level two cache is not required, is a process-level cache controlled by Sessionfactory, which is the developer's own designation. The secondary cache can specify which open-source cache tool to use, Hibernate3 later versions use Ehcache, or they can switch to Oscache, JbossCache. The implementation of using the cache at query time is as follows:
      • Query the first-level cache for data that is required.
    • level Two cache:
      • app and cache
      • distributed cache
      •    

  • sql
  • Span style= "color:black;" >criteria Query
  • HQL:
    • properties Query
    • parameter query, named parameter query
    • associated query
    • paging query
    • statistical functions

       

  • How to optimize hibernate?
    • Use bidirectional one-to-many associations without using
    • Flexible use of unidirectional one-to-many associations
    • You don't have to be a one-to-many substitution
    • Configure the object cache without using the collection cache
    • One-to-many collection using bag, multi-set using Set
    • Inheriting classes using explicit polymorphism
    • Table fields are less, tables associated not afraid of more, there is a level two cache backing

  • struts working mechanism? Why use struts?
    • working mechanism (Struts work flow):

      When the web app starts, it loads initialization Actionservlet,actionservlet reads configuration information from the Struts-config.xml file, stores them in various configuration objects, and when Actionservlet receives a customer request, The following process will be performed.

        • Retrieves the actionmapping instance that matches the user request, and returns invalid request path information if it does not exist;
        • If the Actionform instance does not exist, a Actionform object is created, saving the form data submitted by the customer to the Actionform object;
        • Determines whether form validation is required based on configuration information. If validation is required, call Actionform's validate () method;
        • If Actionform's validate () method returns null or returns a Actuiberrors object that does not contain actionmessage, the form validation succeeds;
        • Actionservlet determines which action to forward the request to, based on the mapping information contained in the actionmapping, and if the corresponding action instance does not exist, create the instance first and then invoke the Execute () method of the action;
        • The Execute () method of the action returns a Actionforward object that actionservlet the JSP component to which the client request is forwarded to the Actionforward object;
        • The Actionforward object points to the JSP component to generate a Dynamic Web page, which is returned to the customer;

      • Why to use:
        • The advent of JSP, Servlet, and JavaBean technology gives us the possibility to build powerful enterprise application systems. But the systems built with these techniques are very fanluan, so on top of that, we need a rule, a rule that organizes these technologies, and that is the framework in which struts emerges.
        • Struts-based applications consist of 3 types of components: Controller components, model components, view components

  • How is the validate framework of struts validated?
    • Configure specific error prompts in the struts configuration file, and then invoke the Validate () method in Formbean.

  • The design pattern of struts
    • MVC mode: Actionservler is loaded and initialized when the Web application starts. When a user submits a form, a configured Actionform object is created and populated with the corresponding data in the form. Actionservler determines whether form validation is required based on the settings configured for the Struts-config.xml file, and if necessary, select the action to which the request is sent if the Validate () validation is called Actionform, if the action does not exist , Actionservlet creates the object first and then invokes the Execute () method of the action. Execute () Gets the data from the Actionform object, completes the business logic, returns a Actionforward object, actionservlet the client request to the JSP component specified by the Actionforward object, The Actionforward object specifies that the JSP generates a dynamic Web page that is returned to the customer.

  • Spring working mechanism and why should it be used?
      • Working mechanism:
        • Spring MVC requests that all requests be submitted to Dispatcherservlet, which will delegate the other modules of the application system responsible for the actual processing of the request.
        • Dispatcherservlet queries one or more handlermapping to find the controller that handles the request.
        • Dispatcherservlet request Submit to target Controller
        • After the controller has processed the business logic, it returns a Modelandview
        • Dispathcher queries one or more viewresolver view resolvers to find the view object specified by the Modelandview object
        • The View object is responsible for rendering back to the client.

      • Why use:
        • AOP allows developers to create non-behavioral concerns, called crosscutting concerns, and insert them into application code. With AOP, public services (such as logs, persistence, transactions, and so on) can be decomposed into facets and applied to domain objects without increasing the complexity of the object model of the domain object.
        • IOC allows you to create an application environment where objects can be constructed, and then pass their collaboration objects to those objects. As the word inversion shows, the IOC is like the reverse JNDI. Not using a bunch of abstract factories, service locators, single elements (singleton), and direct constructs (straight construction), each object is constructed with its collaboration objects. Therefore, the collaboration object (collaborator) is managed by the container.
        • Spring even an AOP framework, is also an IOC container. The best part of Spring is that it helps you replace objects. With Spring, you simply add dependencies (collaboration objects) with the JavaBean property and configuration file. You can then easily replace collaboration objects with similar interfaces when you need them.

  • Spring Layered Architecture
    • The Spring framework is a layered architecture consisting of 7 well-defined modules. The Spring modules are built on top of the core container, and the core container defines how the beans are created, configured, and managed, as shown in 1.
    • each module (or component) that makes up the Spring framework can exist separately or be implemented in conjunction with one or more other modules. The functions of each module are as follows:
      • Core container: The core container provides the basic functionality of the Spring framework. The main component of the core container is Beanfactory, which is the implementation of the factory pattern. The beanfactory uses the inversion of control (IOC) pattern to separate the application's configuration and dependency specifications from the actual application code.
      • Spring Context: The spring context is a configuration file that provides contextual information to the spring framework. The Spring context includes enterprise services such as JNDI, EJB, e-mail, internationalization, checksum scheduling.
      • Spring AOP: With the configuration management feature, the Spring AOP module directly integrates aspect-oriented programming capabilities into the spring framework. Therefore, it is easy to enable any object managed by the Spring framework to support AOP. The spring AOP module provides transaction management services for objects in spring-based applications. By using Spring AOP, you can integrate declarative transaction management into your application without relying on EJB components.
      • The Spring dao:jdbc DAO Abstraction Layer provides a meaningful exception hierarchy that can be used to manage exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the number of exception codes that need to be written (such as opening and closing connections). Spring DAO's JDBC-oriented exception conforms to the common DAO exception hierarchy.
      • The Spring orm:spring Framework inserts several ORM frameworks, providing ORM object-relational tools, including JDO, Hibernate, and IBatis SQL Map. All of these conform to Spring's common transaction and DAO exception hierarchies.
      • Spring Web module: The Web context module is built on top of the application context module and provides the context for Web-based applications. Therefore, the Spring framework supports integration with Jakarta Struts. The Web module also simplifies the process of working with multipart requests and binding request parameters to domain objects.
      • Spring MVC framework: The MVC Framework is a full-featured MVC implementation of building WEB applications. With the policy interface, the MVC framework becomes highly configurable, and MVC accommodates a large number of view technologies, including JSP, Velocity, Tiles, IText, and POI.
      • The functionality of the Spring framework can be used in any Java EE server, and most features are also available in an out-of-management environment. The core point of Spring is to support reusable business and data access objects that are not tied to a particular Java EE service. There is no doubt that such objects can be reused across different Java EE environments (Web or EJB), standalone applications, and test environments.

  • IOC and AOP
      • The basic concept of control reversal mode (also known as dependency intervention) is that you do not create objects, but describe how they are created. The code does not directly connect to objects and services, but describes in the configuration file which component requires which service. Containers (which are IOC containers in the Spring framework) are responsible for linking these together.
      • In a typical IOC scenario, the container creates all the objects and sets the necessary properties to connect them together, deciding when to invoke the method. The following table lists an implementation pattern for the IOC.
      • Aspect-oriented programming, AOP, is a programming technique that allows programmers to modularize the behavior of crosscutting concerns or crosscutting typical lines of responsibility, such as logging and transaction management. The core structure of AOP is the facet, which encapsulates behaviors that affect multiple classes into reusable modules.
      • AOP and IOC are complementary technologies that use a modular approach to solve complex problems in enterprise application development. In a typical object-oriented development approach, logging statements may be placed in all methods and Java classes in order to implement logging functionality. In the AOP approach, the log service can be modularized in turn and applied declaratively to the component that needs the log. Of course, the advantage is that the Java class does not need to know the existence of the log service and does not need to consider the associated code. Therefore, application code written in Spring AOP is loosely coupled.
      • The functionality of AOP is fully integrated into the context of Spring transaction management, logging, and various other features.

      • IOC container
        • The core of the Spring design is the Org.springframework.beans package, which is designed to be used with the JavaBean component. This package is not usually used directly by the user, but is used by the server as the underlying intermediary for most other functions. The next highest-level abstraction is the Beanfactory interface, which is the implementation of the factory design pattern, allowing objects to be created and retrieved by name. Beanfactory can also manage the relationships between objects.

    • The beanfactory supports two object models.
      • A single-state model provides a shared instance of an object with a specific name that can be retrieved at query time. Singleton is the default and most commonly used object model. Ideal for stateless service objects.

      • The prototype model ensures that each retrieval creates a separate object. The prototype model works best when each user needs his or her own object.

    • The Bean Factory concept is the basis of Spring as an IOC container. The IOC shifts responsibility for handling things from application code to frameworks. As I will demonstrate in the next example, the Spring framework uses the JavaBean property and configuration data to indicate the dependencies that must be set.

    • beanfactory interface
      • because Org.springframework.beans.factory.BeanFactory is a simple interface, so it can be implemented for a variety of underlying storage methods. The most commonly used beanfactory definition is xmlbeanfactory, which loads the bean according to the definition in the XML file, as shown in Listing 1.
      • Listing 1. Xmlbeanfactory
        • beanfactory factory = new Xmlbeanfactory (New fileinputs Team ("Mybean.xml"));
        • mybean Mybean = (Mybean) factory.getbean ("Mybean");

    • IOC Example
      • The simplest way to understand control inversion is to look at its practical application. In summarizing the 1th part of the Spring Series, which consists of three parts, I used an example that showed how to inject the application's dependencies through the Spring IOC container (rather than building them in).
      • I use the use case as a starting point for opening an online credit account. For this implementation, opening a credit account requires the user to interact with the following services:
        • Credit rating service, which queries the user's credit history information.
        • Remote information linking service, inserting customer information, connecting customer information to credit card and bank information for automatic debit (if necessary).
        • e-mail Service to send users an email about the status of the credit card.
      • Three Interfaces
        • for this example, I assume that the service already exists, and ideally the situation is to integrate them in a loosely coupled fashion. The following list shows the application interfaces for three services.
          • Listing 3. Creditratinginterface

            Public interface Creditratinginterface {

            public boolean getusercredithistoryinformation (Icustomer icustomer);

            }

          • The credit rating interface shown in Listing 3 provides credit history information. It requires a customer object that contains client information. The implementation of this interface is provided by the Creditrating class.
          • Listing 4. Creditlinkinginterface

            Public interface Creditlinkinginterface {

            Public String getUrl ();

            public void SetUrl (String URL);

            public void Linkcreditbankaccount () throws Exception;

            }

        • The credit link interface connects the credit history information with the bank information (if necessary) and inserts the user's credit card information. The credit link interface is a remote service, and its query is done through the GETURL () method. The URL is set by the Spring framework's bean configuration mechanism, which I'll discuss later. The implementation of this interface is provided by the Creditlinking class.
          • Listing 5. Emailinterface

            Public interface Emailinterface {

            public void SendEmail (Icustomer icustomer);

            Public String getfromemail ();

            public void Setfromemail (String fromemail);

            Public String GetPassword ();

            public void SetPassword (String password);

            Public String getsmtphost ();

            public void Setsmtphost (String smtphost);

            Public String getUserId ();

            public void Setuserid (String userId);

Struts,hibernate,spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.