summarize
Spring is an enterprise-class J2EE application development one-stop solution, which provides functionality throughout the project development of the performance layer, business layer and persistence layer, at the same time, Spring can be seamlessly integrated with other application frameworks.
Spring features include the following:
- Lightweight: Spring is a lightweight framework, the size of its core JAR packages are about 1MB. Spring is also a lightweight framework in terms of system resource usage, requiring only a small amount of operating system resources to run stably during its operation.
- Control Inversion: Spring's control inversion refers to an object dependent on other objects will be in the container after the initialization is completed to actively pass its dependent objects to it, without the need for the object to create or find its own dependent objects.Spring based on control inversion technology to achieve the decoupling of dependencies between the system objects.
- Container-oriented: Spring implements object configuration and object lifecycle management, through Spring's XML files or annotations, the application can configure the time of creation and destruction of each Bean object, as well as the order in which the Bean objects are created and their dependencies.
- Cutting-oriented : Spring provides cutting-oriented programming support , cutting-oriented technology by separating the system logic and business logic to improve system cohesion . In the specific use of the process , the business layer only need to focus on and implement and business-related code logic , and do not need to pay attention to the system functions ( such as system logging , transaction support )
- Modularity: Spring is modular, the application can be used in the process according to the needs of the introduction of modules (in the form of JAR package dependencies) to achieve different functionality
Spring's Core Jar Packages
- Spring Core: Spring's core toolkit
- Spring Beans: the implementation of SpingIoC, through the XML configuration file or annotation to achieve the management of Spring Beans
- Spring Context: Spring context environment. Used for the management of bean relationships, push care and so on.
- Spring Aspects: Spring's Integration and Support for the AspectJ Framework
- Spring Context Support: Extended support for SpringContext to support MVC functionality.
- Spring Expression Language: Spring's Expression Language
- Spring Framework Bom: Handling Version Conflicts Caused by Different Projects Relying on Different Versions of Spring
- Spring JDBC: Spring's wrapper for JDBC
- Spring ORM: Spring integrates with third-party ORM implementations, such as Mybatis.
- Spring Test: Spring's support for testing frameworks such as JUnit.
- Spring TX: Consistent Declarative and Programmatic Transaction Management from Spring
- Spring Web: Core Classes Needed to Build Web Application Development Based on Spring
- Spring WebMVC: Contains all classes related to the SpringMVC framework.
SpringIoC Principles
SpringIoC (Inversion of Control) that is, "Inversion of Control", is a design idea, the creation of objects and the maintenance of dependencies between objects to the container to be responsible for the realization of the object and the object of the loosely coupled
Spring describes the dependencies between a bean and a bean through a configuration file, and uses Java's reflection to instantiate the bean and establish the dependencies between the beans.
Spring will read the bean configuration information provided by the application from the XML configuration file or annotations at startup, and generate a corresponding bean configuration registry in the Spring Bean Container, then instantiate the bean according to this registry, assemble the dependencies between the beans, and put the bean instance into the cache pool, which is implemented using a HashMap
Spring defines five scopes for beans:
- Singleton: Singleton is a singleton pattern, when the instance type is singleton, there will be only one shared instance of the bean in the SpringIoC container, no matter how many beans refer to it, they will always point to the same bean object. This mode is not safe in multithreading, and the Singleton scope is the default scope in Spring.
- Prototype: Prototype is a prototype pattern. Each time a Prototype-defined bean is fetched through the Spring container, the container creates a new instance of the bean, each with its own properties and state. Therefore, Prototype scopes are often used for stateful beans, while Singleton scopes are used for stateless beans.
- Request: Request means that the container will return the same instance of the bean in one HTTP request, a new instance of the bean will be created for a different HTTP request, and the instance of the bean will only be valid for the current HTTP request, and the instance of the bean will be destroyed at the end of the current HTTP request.
- Session: Session means that in one HTTP session, the container will return the same instance of the bean, and for different session requests, a new instance of the bean will be created, which is only valid within the current session, and the session creates a new instance of the bean every time, and different instances of the bean do not share data with each other, and the request ends, the bean instance will be destroyed. Session creates a new instance of the bean each time, and the different instances of the bean do not share data with each other, and when the request ends, the instance of the bean is destroyed.
- Global Session: A Global Session is similar to a Session, but is only used in Portlet Web applications. The portlet specification defines the concept of a global session that is shared among all the portlets that make up a single Portlet Web application. If it is not a Portlet Web application, it is the same as a Session.
The life cycle of a Spring bean is shown below:
- Instantiating a Bean
- Configure the instantiated bean according to Spring contexts
- Execution is loaded according to the configuration:
- If the bean implements the BeanNameAware interface, it executes its implementation of the setBeanName(String) method. The parameter passed to this method is the id value of the bean in the Spring configuration file
- If the bean implements the BeanFactoryAware interface, it executes its implementation of the setBeanFactory(BeanFactory) method, which is passed the parameters of the Spring factory itself.
- If the bean implements the ApplicationContextAware interface, it executes the setApplicationContext(ApplicationContext) method, which passes a parameter of the Spring context
- If the bean is associated with the BeanPostProcessor interface, the postProcessBeforeInitialization(Object obj, String s) method will be executed, which is called before the initialization of the bean, and is often used to define the pre-initialization of the bean, such as the initialization of the system cache.
- If the bean is configured with the init-method attribute in the Spring configuration file, its configured initialization method is executed automatically
- If the bean is associated with the BeanPostProcessor interface, the postProcessAfterInitialization(Object obj, String s) method will be executed, and the initialization of the bean will be completed, and the application can start using the bean instance.
- When the bean is no longer needed, it is cleaned up during the cleanup phase, and if the bean implements the DisposableBean interface, Spring calls the destroy method of the implementing class before exiting.
- If the destroy-method attribute is configured in the bean's Spring profile, its configured destruction method is automatically invoked before the bean is destroyed
SpringAOP Principles
SpringAOP through the cutter-oriented technology will have nothing to do with the business but for the business module to share the logic code encapsulated . In order to improve code reuse and reduce the coupling between modules.
The core SpringAOP concepts are as follows:
- Cross-cutting concerns: defining which methods are blocked, and which actions are performed after blocking.
- Aspect: abstraction of cross-cutting concerns
- JoinPoint: the method that is intercepted.
- PointCut: Definition of an interception stamp for a connection point
- Advice: the specific operation to be performed after intercepting the connection point, categorized into pre-notification, post-notification, success notification, exception notification and wrap-around notification.
- Target object: the target object of the agent
- Weave: applies a cutout to a target object and performs proxy creation.
- Introduction: Dynamically add methods or fields to a class at runtime without modifying the class code.
SpringAOP has five notification types:
- Pre-Notification: performs notification before a method is executed
- Posterior notification: performs notification after a method is executed, regardless of whether the method execution succeeds or fails.
- Success notification: performs notification after a method has been successfully executed, and only when the method has been successfully executed
- Exception notification: executed when a method throws an exception
- Wrap-around notification: executed before and after the interception method call respectively
Spring provides both JDK and CGLib to generate proxy objects, the specific generation method is determined by the AopProxyFactory based on the configuration of the AdvisedSupport object.Spring's default proxy generation strategy: if it is a target class interface, then use JDK dynamic proxy technology, otherwise use CGLib dynamic proxy technology if it is a target class interface, otherwise it uses CGLib dynamic proxy technology.
Difference between CGLib dynamic proxies and JDK dynamic proxies: JDK can only create proxy instances for interfaces, and for classes that do not have business methods defined through interfaces, they can only be realized by creating dynamic proxies through CGLib.
SpringMVC Principles
MVC in SpringMVC is Model-View-Controller, which is based around a DispatcherServlet that distributes requests to various processors and supports configurable processor mapping and view rendering.
The workflow of SpringMVC is shown below:
- The client initiates the HTTP request: the client submits the request to the DispatcherServlet
- Finding the Handler: The DispatcherServlet controller queries one or more HandlerMapping to find the Controller that handled the request
- Call Processor: DispatcherServlet submits the request to the Controller.
- Call the business processing logic and return the result: Controller returns the ModelAndView after calling the business processing logic.
- Process the view mapping and return the model: the DispatcherServlet queries one or more ViewResolver view resolvers to find the view specified by ModelAndView
- HTTP Response: The view is responsible for coloring and displaying the results on the client browser.