Interview - Spring+SpringMVC

1. What is Spring and what modules are there?

Spring is an open source framework that helps developers solve fundamental problems in development, allowing developers to focus on application development.

Spring framework has integrated more than 20 modules so far. These modules are mainly divided into core container, data access/integration, Web, AOP (Aspect Oriented Programming), tools, message and test modules as shown in the figure below.

IOC is inversion of control. We give the right to create objects to the framework, and inject them to us through dependency injection when we need to use them;

AOP is aspect-oriented programming. Its main idea is to extract horizontally repetitive code vertically. Spring implements AOP through JDK dynamic proxy (must implement the same interface, first) and cglib proxy (as long as it can be inherited).

  • Good isolation and coupling between individual steps is greatly reduced 

  • The source code is independent, and the source code is not modified when the function is extended. 

2. Spring's common annotations for creating objects?

@Component@Controller@ Service@ Repository 

3. Design patterns used in Spring

Answer: Simple Factory, Factory Method, Singleton Pattern, Adapter, Wrapper, Proxy, Observer, Strategy, Template Method

4. What are the advantages of Spring?

  • 1. Reduce the coupling between components and realize the decoupling between various layers of the software (IOC, objects are created through reflection)
  • 2. Can use the many services provided , such as transaction management, message services, etc.
  • 3. The container provides singleton mode support (benefits: save system resources)
  • 4. The container provides AOP technology , and it is easy to use it to implement functions such as permission interception and runtime monitoring. 
  • 5. The container provides many auxiliary classes, which can speed up application development 
  • 6.Spring provides integrated support for mainstream application frameworks , such as hibernate, JPA, Struts, etc. 
  • 7. Spring is a low-intrusive design, and the pollution of the code is extremely low 
  • 8. Independent of various application servers 
  • 9. Spring's DI mechanism reduces the complexity of business object replacement 
  • 10. The high degree of openness of Spring does not force the application to completely depend on Spring, and developers can freely choose part or all of spring 

5. What is the difference between the scopes of Spring Beans?

The beans in the Spring container can be divided into 5 scopes. All scope names are self-explanatory, but to avoid confusion let's explain:

singleton: This bean scope is the default. This scope ensures that no matter how many requests are received, there is only one instance of the bean in each container. The singleton pattern is maintained by the bean factory itself.

prototype: The prototype scope is the opposite of the singleton scope, providing an instance for each bean request. 

request: An instance is created within the scope of the request bean for each network request from the client. After the request is completed, the bean is invalidated and recycled by the garbage collector. 

Session: Similar to the request scope, ensure that there is an instance of the bean in each session, and the bean will be invalidated after the session expires.

global-session: The global-session is related to the Portlet application. When your application is deployed to work in a portlet container, it contains many portlets. If you want to declare a global storage variable that is shared by all portlets, then this global variable needs to be stored in the global-session.

The global scope has the same effect as the session scope in the servlet.

6. How many ways does Spring manage transactions?

A: There are two ways:

1. Programmatic transactions, hardcoded in the code. (deprecated)

2. Declarative transactions, configured in the configuration file (recommended)

There are two types of declarative transactions: 

  • a, XML-based declarative transactions
  • b. Annotation-based declarative transactions

7. What are the methods of automatic assembly in spring?

Answer: 1. No: that is, automatic assembly is not enabled. 

2. byName: Find and inject objects that JavaBean depends on by the name of the property. For example, the class Computer has an attribute printer. After specifying its autowire attribute as byName, the Spring IoC container will look for a bean whose id/name attribute is printer in the configuration file, and then use the Seter method to inject it.

3. byType: Find the object that JavaBean depends on by the type of the attribute and inject it. For example, the class Computer has an attribute printer and the type is Printer. Then, after specifying its autowire attribute as byType, the Spring IoC container will look for a bean whose Class attribute is Printer, and use the Seter method to inject it.

4. Constructor: Like byType, it also searches for dependent objects by type. The difference from byType is that instead of using Seter method injection, it uses constructor injection.

5. autodetect: Automatically select the injection method between byType and constructor.

6. default: determined by the default-autowire attribute of the parent tag <beans>.

8. What are the core classes in spring and what are the functions of each?

Answer: BeanFactory: Generate a new instance, which can implement the singleton pattern

BeanWrapper: Provides unified get and set methods

ApplicationContext: Provides the implementation of the framework, including all the functions of BeanFactory 

9. What are the calling methods of Bean?

Answer: There are three ways to get a bean and call it: 

3. Use ApplicationContext (create Spring container according to configuration file, then get bean) 

ApplicationContext actx=new FleSystemXmlApplicationContext(”config.xml”); 

HelloWorld hw=(HelloWorld) actx.getBean(”HelloWorld”); 

System.out.println(hw.getMsg()); 

10. What is IOC and what is DI, what is the difference between them?

Answer: Dependency Injection DI is a programming pattern and architectural model, sometimes called Inversion of Control, although technically speaking, Dependency Injection is a special implementation of IOC. Dependency injection refers to the application of an object to another object to provide A special ability, for example, to pass a database connection as a parameter to an object's struct method instead of creating a connection within that object itself. The basic idea of ​​Inversion of Control and Dependency Injection is to transform the dependencies of a class from inside the class to the outside to reduce dependencies.

Using inversion of control, when an object is created, an external entity that controls all objects in the system passes to it the references to the objects it depends on. It can also be said that dependencies are injected into objects. So, Inversion of Control is the inversion of responsibility for how an object obtains references to objects it depends on.

11. Spring has two proxy methods:

Answer: If the target object implements several interfaces, spring uses the java.lang.reflect.Proxy class of JDK to proxy.

      Advantages: Because of the interface, the system is more loosely coupled

      Disadvantage: create interface for each target class

If the target object does not implement any interface, spring uses the CGLIB library to generate a subclass of the target object.

      Advantages: Because the proxy class and the target class are inherited, there is no need for the existence of an interface.

      Disadvantages: Because no interface is used, the coupling of the system is not as good as that of using JDK's dynamic proxy.

12. The process of springMVC (request life cycle)?

  • 1. The user sends a request to the front-end controller DispatcherServlet
  • 2. The DispatcherServlet receives the request and calls the HandlerMapping handler mapper.
  • 3. The processor mapper finds a specific processor according to the request url, generates a processor object and a processor interceptor (if any) and returns it to the DispatcherServlet.
  • 4. The DispatcherServlet calls the handler through the HandlerAdapter handler adapter
  • 5. Execute the processor (Controller, also called the back-end controller).
  • 6. Controller execution completes and returns to ModelAndView
  • 7. HandlerAdapter returns the controller execution result ModelAndView to DispatcherServlet
  • 8. DispatcherServlet passes ModelAndView to ViewReslover view resolver
  • 9. ViewReslover returns to the specific View after parsing
  • 10. The DispatcherServlet renders the View (that is, fills the model data into the View).
  • 11. DispatcherServlet responds to the user

13. Advantages of Springmvc

1. It is based on component technology. All application objects, whether controllers and views, or business objects, are java components. And they are tightly integrated with other infrastructure provided by Spring.

2. Does not depend on the Servlet API (the goal is the same, but it does depend on the Servlet when it is implemented)

3. Various view technologies can be used arbitrarily, not just limited to JSP

4. Support mapping strategies for various requested resources

5. It should be easily extensible

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324668220&siteId=291194637