[Notes] "Spring Combat" - Spring Tour

        Spring can do many things, and it provides a wealth of features for enterprise-level development. But the bottom layer of these functions relies on its two core features, namely dependency injection (DI) and aspect-oriented programming (AOP).

1. Simplify Java development

        Spring's most fundamental mission: to simplify Java development.

        In order to reduce the complexity of Java development, Spring adopts the following 4 key strategies:

  • Lightweight and minimally invasive programming based on POJOs;
  • Loose coupling through dependency injection and interface orientation;
  • Declarative programming based on aspects and management;
  • Reduce boilerplate code with aspects and templates.

1. Activate the potential of POJO

        Spring strives to avoid cluttering your application code with its own API. Spring doesn't force you to implement Spring-specific interfaces or extend Spring-specific classes. On the contrary, in applications built on Spring, its classes usually have no trace of your use of Spring. The worst case scenario is that a class might be annotated with Spring, but it's still a POJO.

        One of the ways Spring brings magic to POJOs is to wire them through DI.

2. Dependency Injection

        Through DI, the dependencies of objects are set when the objects are created by the third-party components in the system responsible for coordinating the objects. Objects do not need to create or manage their dependencies on their own. Dependencies will be automatically injected into objects that need them.

        If an object expresses a dependency only through an interface (rather than a concrete implementation or initialization process), that dependency can be replaced with a different concrete implementation without the object's knowledge.

        One of the most common ways to replace dependencies is to use mock implementations when testing.

        Spring loads bean definitions and assembles them through the Application Context. The Spring application context is solely responsible for the creation and assembly of objects. Spring comes with a variety of application context implementations, the main difference between them is how to load the configuration.

3. Apply Aspects

        Aspect-oriented programming (AOP) allows you to separate out functionality throughout your application into reusable components.

        Oriented programming is often defined as a technique that enables software systems to achieve separation of concerns. A system consists of many different components, each responsible for a specific piece of functionality. In addition to implementing their own core functionality, these components often have additional responsibilities. System services such as injection logging, transaction management, and security are often incorporated into components that have their own core business logic. These system services are often referred to as cross-cutting concerns because they span multiple components of the system.

        If you spread these concerns across multiple components, your code will introduce double complexity.

  • The code that implements the system concerns functionality will be repeated across multiple components. This means that if you want to change the logic of these concerns, you must modify the relevant implementation in each module. Even if you abstract these concerns into a separate module, and other modules just call its methods, the method calls will still be repeated across modules.
  • Components get cluttered with code that is not related to their core business. A method of adding address entries to the address book should only be concerned with how the address is added, not whether it is secure or whether it needs to support transactions.

4. Use templates to remove boilerplate code

        Spring aims to eliminate boilerplate code through template encapsulation. Spring's JdbcTemplate makes it possible to avoid traditional JDBC boilerplate code when performing database operations.

2. Accommodate your Bean

        In a Spring-based application, your application objects live in the Spring container. The Spring container is responsible for creating objects, assembling them, configuring them and managing their entire declaration cycle, from life to death.

        The container is the heart of the Spring framework. The Spring container uses DI to manage the components that make up the application, and it creates associations between components that work together. There is no doubt that these objects are simpler and cleaner, easier to understand, easier to reuse and easier to unit test.

        There is not only one Spring container. Spring comes with multiple container implementations, which can be grouped into two different types. A bean factory is the simplest container and provides basic DI support. The application context is built on BeanFactory and provides application framework-level services, such as parsing textual information from properties files and publishing application events to interested event listeners.

1. Use application context

Spring comes with several types of application contexts.

  • AnnotationConfigApplicationContext: Loads the Spring application context from one or more Java-based configuration classes.
  • AnnotationConfigWebApplicationContext: Loads the Spring web application context from one or more Java-based configuration classes.
  • ClassPathXmlApplicationContext: Load context definitions from one or more XML configuration files in the classpath, and use the application context definition files as class resources.
  • FileSystemXmlApplicationContext: Loads context definitions from one or more XML configuration files under the file system.
  • XmlWebApplicationContext: Loads context definitions from one or more XML configuration files under the web application.

2. bean life cycle

 

Guess you like

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