Spring hibernate struts

  • 2020-05-07 19:41:57
  • OfStack

This article describes the classic interview questions of Spring,hibernate,struts and their reference answers. Share with you for your reference, as follows:

How does work and why does Hibernate work?

Principle:

1. Read and parse the configuration file
2. Read and parse the mapping information to create SessionFactory
3. Open the Sesssion
4. Create transaction Transation
5. Persist operations
6. Commit transactions
7. Close the Session
8. Close SesstionFactory

Why use:

. To JDBC access to the database code to do packaging, greatly simplified the data access layer tedious repetitive code.

Hibernate is a mainstream persistence framework based on JDBC and an excellent implementation of ORM. It greatly simplifies the coding of the DAO layer

hibernate USES Java reflection instead of bytecode enhancer to achieve transparency.

The performance of hibernate is very good 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.

2. How Hibernate is lazily loaded

Hibernate2 lazy load implementation: a) entity object b) collection (Collection)

Hibernate3 provides lazy loading of attributes
When Hibernate queried the data, the data did not exist in memory. When the program actually operated on the data, the object existed in memory, which realized lazy loading. It saved the memory overhead of the server and thus improved the performance of the server.

How to implement relationships between classes in 3. (e.g. 1 to many, many to many)

The relationship between classes is mainly reflected in the operation of the relationship between tables. They operate on objects. In our program, all the tables and classes are mapped in 1.

4. Talk about Hibernate's caching mechanism

. Internal cache exists in Hibernate, also known as level 1 cache, belongs to the application level cache

Ii. Level 2 cache:

a) applications and caching
b) distributed cache
Conditions: the data will not be modified by the third party, the data size is in the acceptable range, the data update frequency is low, the same data is frequently used by the system, non-critical data
c) implementation of the 3rd party cache

5. Hibernate query

Sql, Criteria object comptosition

Hql:

1. Attribute query
2, parameter query, named parameter query
3. Correlation query
4. Paging queries
5. Statistical function

6. How to optimize Hibernate?

. Use two-way 1 to many association, do not use one-way 1 to many
Ii. Flexible use of one-way 1 - to - many association
(3) instead of 1 to 1, replace it with many-to-1
. Configure the object cache, do not use the collection cache
1. Bag is used for multiple sets, and Set is used for many-to-many sets
6. Inheritance classes use explicit polymorphism
7. Table fields to less, table association do not be afraid of more, there are 2 levels of cache support

7. How does Struts work? Why Struts?

Working mechanism:

Struts workflow:

When the web application starts, ActionServlet is loaded, and ActionServlet is initialized

The struts-config.xml file reads the configuration information and stores it in various configuration objects

When ActionServlet receives a customer request, the following process is performed.

-(1) retrieve the ActionMapping instance that matches the user's request, and return the invalid information of the request path if it does not exist;
-(2) if the ActionForm instance does not exist, create an ActionForm object and save the form data submitted by the customer into the ActionForm object;
-(3) determine whether form validation is required based on the configuration information. If validation is required, the validate() method of ActionForm is called;
-(4) if the validate() method of ActionForm returns null or an ActuibErrors object that does not contain ActionMessage, the form validation is successful;
-(5)ActionServlet decides to forward the request to Action based on the mapping information contained in ActionMapping. If the corresponding Action instance does not exist, this instance is first created and then execute() method of Action is called;
-(6) execute() method of Action returns an ActionForward object,ActionServlet is forwarding the customer request to JSP component pointed by ActionForward object;
-(7)ActionForward object generates a dynamic web page to the JSP component and returns it to the customer;

Why use:

The emergence of JSP, Servlet, JavaBean technologies makes it possible to build powerful enterprise applications. But the system built with these technologies was very messy, so on top of that, we needed a rule, a rule to organize these technologies, and that was the framework, and Struts was born.

The application developed based on Struts consists of three types of components: controller component, model component and view component

8. How is Struts's validate framework validated?

Configure the specific error in the struts configuration file, and then call the validate() method in FormBean.

9. Talk about Struts's design pattern

MVC mode: the web application loads and initializes ActionServler when it starts. When the user submits the form, one configured ActionForm object is created, and fill in the form of the corresponding data, according to Struts ActionServler - config. xml file configured Settings to decide whether to need a form validation, if you need to call the ActionForm Validate () after the validation choose which Action sends a request to, and if Action does not exist, ActionServlet will first create the object, then call Action execute () method. Execute () gets data from ActionForm object, completes the business logic, returns an ActionForward counter, ActionServlet forwards the customer request to jsp component specified by ActionForward object, ActionForward object specified by jsp generates a dynamic web page and returns it to the customer.

10. spring and why it works ?

spring mvc requests that all requests be submitted to DispatcherServlet, which delegates the actual processing of requests to other modules of the application system.
DispatcherServlet looks up one or more HandlerMapping and finds the Controller that processed the request.
Please request to submit DispatcherServlet to target Controller
. After the business logic processing of Controller, 1 ModelAndView will be returned
Dispathcher queries one or more ViewResolver view parsers to find the view object specified by the ModelAndView object
. The view object is responsible for rendering back to the client.

Why use:

{AOP} lets developers create non-behavioral concerns, called crosscutting concerns, and insert them into application code. With AOP, common services (such as logs, persistence, transactions, and so on) can be decomposed into aspects and applied to domain objects without increasing the complexity of the object model for domain objects.

IOC allows you to create an application environment that can construct objects and then pass their collaboration objects to those objects. As the word inverted indicates, IOC is like JNDI inverted. Instead of using a heap of abstract factories, service locators, single elements (singleton), and direct constructs (straight construction), each object is constructed with its collaboration object. So the collaboration object is managed by the container (collaborator).

Even an AOP framework is an IOC container. The best thing about Spring is that it helps you replace objects. With Spring, just add dependencies (collaboration objects) with JavaBean properties and configuration files. Collaboration objects with similar interfaces can then be easily replaced as needed. }

The Spring framework is a layered architecture consisting of seven well-defined modules. The Spring module is built on top of the core container, which defines how bean is created, configured, and managed.

Each module (or component) that makes up the Spring framework can be implemented separately or in conjunction with one or more other modules. The functions of each module are as follows:

☆ core containers: core containers provide the basic functionality of the Spring framework. The main component of the core container is BeanFactory, which is an implementation of the factory pattern. BeanFactory USES the inversion of control (IOC) pattern to separate the application's configuration and dependency specifications from the actual application code.
☆ Spring context: an Spring context is a configuration file that provides context information to the Spring framework. The Spring context includes enterprise services, such as JNDI, EJB, E-mail, internationalization, checksum scheduling.
Spring AOP: the Spring AOP module integrates aspect-oriented programming directly into the Spring framework through configuration management features. As a result, it is easy to make AOP supported by any object managed by the Spring framework. 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 applications without relying on the EJB component.
The Spring DAO: JDBC DAO abstraction layer provides a meaningful exception hierarchy to manage exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the amount of exception code that needs to be written (such as opening and closing connections). Spring DAO's exceptions for JDBC comply with the common DAO exception hierarchy.
ORM: the Spring framework provides object relationship tools for ORM by inserting several ORM frameworks, including JDO, Hibernate, and iBatis SQL Map. All of this complies with Spring's generic transaction and DAO exception hierarchies.
Spring Web module: the Web context module is built on top of the application context module to provide context for Web based applications. Therefore, the Spring framework supports integration with Jakarta Struts. The Web module also simplifies processing multipart requests and binding request parameters to domain objects.
Spring MVC framework: the MVC framework is a fully functional MVC implementation of building Web applications. The MVC framework becomes highly configurable through the policy interface, and MVC houses a number of view technologies, including JSP, Velocity, Tiles, iText, and POI.

The functionality of the Spring framework can be used on any J2EE server, and most of the functionality is also available in an unmanaged environment. At the heart of Spring is support for reusable business and data access objects that are not bound to specific J2EE services. Undoubtedly, such objects can be reused between different J2EE environments (Web or EJB), standalone applications, and test environments.

IOC and AOP

The basic concept of the inversion of control pattern (also known as dependency intervention) is not to create objects, but to describe how to create them. It is not directly wired to objects and services in the code, but in the configuration file it describes which components need which services. The container (in the Spring framework, the IOC container) is responsible for making these connections in 1.

In a typical IOC scenario, the container creates all the objects, sets the necessary properties to connect them in 1, and decides when to call the method. The following table lists one of the implementation patterns for IOC.

The IOC container of the Spring framework is implemented using types 2 and 3.

Aspect oriented programming

Aspect-oriented programming, or AOP, is a programming technique that allows programmers to modularize crosscutting concerns or behavior that crosscuts typical lines of responsibility, such as logging and transaction management. The core construct of AOP is the aspect, which encapsulates the behavior that affects 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 might be implemented by placing logging statements in all methods and the Java class. In the AOP approach, log services can be modularized in turn and applied declaratively to components that require logging. The advantage, of course, is that the Java class does not need to know about the existence of the logging service or consider the associated code. As a result, the application code written in Spring AOP is loosely coupled.

AOP's capabilities are fully integrated into the context of Spring's transaction management, logging, and various other features.

IOC container

At the heart of the Spring design is the org.springframework.beans package, which is designed to work with the JavaBean component 1. This package is typically not used directly by the user, but is used by the server as an underlying mediation for most other functions. The next highest level abstraction is the BeanFactory interface, which is an implementation of the factory design pattern, allowing objects to be created and retrieved by name. BeanFactory also manages relationships between objects.

BeanFactory supports two object models.

□ the singleton 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 a separate object is created for each retrieval. The prototype model works best when each user needs their own object.

The bean factory concept is the basis for Spring as the IOC container. IOC shifts the responsibility for handling things from the application code to the framework. As I will demonstrate in the next example, the Spring framework USES the JavaBean properties and configuration data to indicate dependencies that must be set.

BeanFactory interface

Because org. springframework. beans. factory. BeanFactory is a simple interface, so it can be for a wide variety of underlying storage method. The most common BeanFactory definition is XmlBeanFactory, which loads bean according to the definition in the XML file, as shown in listing 1.

Listing 1. XmlBeanFactory


BeanFactory factory = new XMLBeanFactory(new FileInputSteam("mybean.xml")); 

Bean as defined in the XML file is loaded negatively, which means that bean itself is not initialized until bean is needed. To retrieve bean from BeanFactory, simply call the getBean() method and pass in the name of the bean to be retrieved, as shown in listing 2.

Listing 2. getBean ()


MyBean mybean = (MyBean) factory.getBean("mybean"); 

The definition of each bean can be either POJO (defined with the class name and the JavaBean initialization property) or FactoryBean. The FactoryBean interface adds a level of indirection to applications built using the Spring framework.

IOC sample

The easiest way to understand inversion of control is to see it in action. In concluding part 1 of the three-part Spring series, I used an example that demonstrated how to inject application dependencies (rather than building them in) through the Spring IOC container.

I use the use case for opening an online credit account as a starting point. For this implementation, opening a credit account requires the user to interact with the following services:

☆ credit rating service, inquiry user's credit history information.
☆ remote information link service to insert customer information and connect customer information with credit card and bank information for automatic debit (if necessary).
☆ an E-mail service that sends users e-mails about the status of their credit CARDS.

three interfaces

For this example, I assume that the services already exist, ideally integrating them in 1 in a loosely coupled manner. The following listing shows the application program interfaces for the 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 an Customer object that contains customer 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 to the bank information (if needed) at 1 and inserts the user's credit card information. The credit link interface is a remote service whose query is performed through the getUrl() method. URL is set by the bean configuration mechanism of the Spring framework, which I will 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);

I hope that this article is helpful to you Java programming.


Related articles: