In-depth understanding of spring face-to-face

1 Do you understand the processing flow of SpringMVC?

  • The user sends a request to the front controller DispatcherServlet.
  • DispatcherServlet finds the corresponding processor through the processor mapper HandlerMapping.
  • DispatcherServlet submits the request to the corresponding processor Controller.
  • Controller returns to ModelAndView after processing the request.
  • DispatcherServlet passes ModelAndView to ViewResolver for resolution.
  • Finally, DispatcherServlet will respond to user requests and display the corresponding views.

1.1 1. Is the native method stack of jvm private to each thread?

Yes, each thread has its own private Java virtual machine stack (Java Stack) and local method stack (Native Method Stack). When a thread is created, its stack is also created. The Java Stack is mainly used to store the frame of the Java method (Frame), while the Native Method Stack is used to support the execution of the Native method.

  1. What is the relationship between SpringMVC and Tomcat?

    • Tomcat : Tomcat is a web container, or a Servlet container, used to provide an environment in which Java web applications (mainly Servlets, JSPs, Filters, etc.) can run and serve HTTP requests.

    • SpringMVC : SpringMVC is a module of the Spring framework that implements the MVC (Model-View-Controller) design pattern for building web applications. Specifically, it is a front controller (Front Controller) framework for web applications that can handle HTTP requests and decide which Controller (processor) handles which request based on the developer's configuration.

    relationship :

    • When an HTTP request arrives at Tomcat, Tomcat will decide which Servlet to process the request according to the configuration. If the web application is based on SpringMVC, the request is usually sent to DispatcherServlet(SpringMVC's core Servlet).
    • DispatcherServletResponsible for processing the request, and decide which Controller method to distribute the request to for processing according to the developer's definition in the Spring configuration.
    • So in simple terms, Tomcat provides a running environment for SpringMVC, and SpringMVC manages and processes Web requests in this environment.

1.2 What is the relationship between the threading model used in tomcat and springMVC here?

  • Tomcat threading model: Tomcat uses a thread pool to process incoming HTTP requests. Whenever a new request arrives, Tomcat takes a thread from the thread pool to process the request. This means that concurrent requests will be processed by concurrent threads.

  • Relationship: When a thread in Tomcat processes a request to SpringMVC, the thread enters SpringMVC's DispatcherServlet, and then is further routed to the corresponding Controller. This means that SpringMVC's Controller methods usually run in Tomcat's request processing thread. Therefore, SpringMVC's execution model and performance are largely influenced by Tomcat's threading model.

2 What are the main differences between Java versions 1.7 and 1.8?

  • Lambda expressions: Java 8 introduces Lambda expressions, adding a whole new way of programming to Java.
  • Stream API: Java 8 adds a new Stream API to support data processing.
    Default and static methods in interfaces: In Java 8, interfaces can have method implementations by using the default keyword.
  • New Date and Time API: Java 8 introduces a completely redesigned Date and Time API.
    Optional class: Java 8 introduced the Optional class, which is a container object that can hold null values.
  • Nashorn JavaScript Engine: In Java 8, the JVM got a new Nashorn JavaScript engine, making it possible to run JavaScript on the JVM.
    New Collector: Java 8 introduces many new methods in the Collectors class to support advanced aggregation operations.
  • The features of Java 7: such as Switch-String, try-with-resources, diamond operator, new File API, etc., these still exist in Java 8, but the new features of Java 8 compared to Java 7 are listed above.

3 Do you understand the principle of Spring AOP?

Spring AOP (Aspect-Oriented Programming) uses proxy mode, mainly implemented in two ways:

  • JDK dynamic proxy: Spring AOP uses JDK's dynamic proxy to create a proxy when the target class implements an interface. This involves the java.lang.reflect.Proxy class and the java.lang.reflect.InvocationHandler interface.

  • CGLIB proxies: Spring AOP uses the CGLIB library to create class-based proxies when the target class does not implement an interface.

In both cases, the created proxy intercepts calls to the target object's methods, allowing us to inject custom logic such as logging, transaction management, security checks, etc. before, after calling the method, or when an exception is raised.

4 What problem does Spring AOP mainly want to solve?

Spring AOP aims to provide aspect-oriented programming capabilities to solve the following problems:

  • Cross-cutting concerns: Common functions (such as logging, transaction management, security checks, etc.) that exist in multiple modules or functions are called cross-cutting concerns. These cross-cutting concerns are often intermingled with business logic, resulting in code duplication and difficulty in maintaining .

  • Modularity: AOP allows us to make these cross-cutting concerns modular and independent of business logic. This way, we can centrally manage these concerns and easily modify or add functionality when needed.

  • Code is clear and maintainable: By separating business logic from cross-cutting concerns, we can make business code more clear, concise and maintainable.

In short, the goal of Spring AOP is to provide a mechanism that enables developers to separate common, repetitive functions (such as logging, security, transactions, etc.) from business logic, thereby improving code maintainability, readability, and reuse sex.

5 Spring bean and application life cycle?

5.1 The life cycle of Spring Bean:

  1. Bean definitions are read from XML configuration files and instantiated.
    Spring performs dependency injection on bean properties.
  2. If the Bean implements the BeanNameAware interface, Spring passes the bean's ID to the setBeanName() method.
  3. If the Bean implements the BeanFactoryAware interface, Spring will call the setBeanFactory() method and pass in the BeanFactory.
  4. If the Bean implements the ApplicationContextAware interface, the setApplicationContext() method will be called, passing in the current ApplicationContext.
  5. If the Bean implements the BeanPostProcessor interface, the postProcessBeforeInitialization() method will be called.
  6. If the Bean implements the InitializingBean interface, the afterPropertiesSet() method will be called.
  7. If the bean declares an initialization method using init-method in the configuration file, this method will be called.
  8. If the Bean implements the BeanPostProcessor interface, the postProcessAfterInitialization() method will be called.
  9. The bean is now ready to be used by the application.
  10. When the container is closed, if the Bean implements the DisposableBean interface, the destroy() method will be called.
  11. If the bean declares a destruction method using destroy-method in the configuration file, this method will be called.

5.2 The life cycle of Spring Application:

  1. ApplicationContext is initialized/refreshed.
  2. Bean instantiation, configuration and invocation of various initialization methods.
  3. ApplicationContext publishes ContextRefreshedEvent event.
  4. When the application is running, the ApplicationContext and the Beans in it can be used.
  5. Beans may be destroyed when the ApplicationContext is closed.
  6. ApplicationContext publishes ContextClosedEvent event.

6 MyBatis is a popular Java ORM framework that uses a variety of design patterns, including:

  • Factory Pattern: Use SqlSessionFactory to create SqlSession.
  • Builder pattern (Builder Pattern): such as XMLConfigBuilder and XMLMapperBuilder, used to build and parse configuration files.
  • Template Pattern: This pattern is used in some methods of MyBatis' internal processing of database operations. It defines the steps of the operation, but postpones the implementation of some steps.
  • Proxy Pattern: MyBatis uses JDK dynamic proxy to create a proxy object for the Mapper interface, so that developers can directly call database operations through the interface without writing an implementation.
  • Singleton Pattern: Like Configuration, there is usually only one instance of each bean in the entire MyBatis session.
  • Composite Pattern: In the configuration file, there can be multiple combinations.

7 What capabilities does volatile achieve, and how?

8 Spring Boot's automatic configuration is based on the following core concepts:

  • @EnableAutoConfiguration: This is the core annotation of Spring Boot's automatic configuration. When you add the @SpringBootApplication annotation to the main class, it actually includes @EnableAutoConfiguration. This annotation tells Spring Boot to auto-configure the project based on the added jar dependencies.

  • The spring.factories file: The spring.factories file plays a central role in Spring Boot's auto-configuration process. Spring Boot scans the classpath of the project at startup, finds all jar packages containing spring.factories files, then reads the contents of the files, finds and loads all auto-configuration classes.

  • Conditional annotations: such as @ConditionalOnClass, @ConditionalOnBean, @ConditionalOnMissingBean, @ConditionalOnProperty, etc. These annotations ensure that certain configurations or beans are only created if certain conditions are met. For example, if there is a certain class on the classpath, or if there is/is not a certain bean in the Spring context, or if a configuration property has a specific value, etc.

The overall process is briefly as follows:

  • Spring Boot scans the classpath at startup and finds the jar package containing the spring.factories file.
  • All auto-configuration classes are obtained from the spring.factories file.
  • Based on conditional annotations, decide which auto-configuration classes will be loaded or which beans will be created.
  • The default configuration can also be adjusted through the application.properties or application.yml files if needed.

Guess you like

Origin blog.csdn.net/yxg520s/article/details/132211387