Excellent Java backend framework. You can reduce the coupling of applications by handing over objects to Spring for management. Make your code more refined with AOP.

IOC

Inversion Of Control. Give the framework the power to create objects and reduce code coupling (reducing dependencies between programs). Dependencies are managed by Spring. The current class needs to use other class objects, as explained in the configuration file, provided by Spring.

Bean

Bean! == Java entity class. Beans are reusable components.

There are three ways to create beans

  • Create objects using the default constructor.
  • Create objects using the normal factory method.
  • Create objects using static sides in the factory.

Bean scope adjustment

The scope attribute

  • Singleton: singleton (default).
  • Prototype: many examples.
  • Request: Range of requests applied to web applications.
  • Session: The range of sessions applied to the Web application.
  • Global-session: specifies the session scope that applies to the cluster environment (the global session scope, if not the cluster environment, is the session).

The life cycle of the Bean

  • singleton

Born: The object is born when the Spring container is created.

Alive: The container is still there and the object is still there.

Death: The container dies and the object dies.

Summary: Same as container.

  • Many cases of object

Birth: Created by Spring when objects are used.

Alive: The object is alive as long as it is in use.

Dead: Spring doesn’t know when an object runs out, so Java’s GC collects it.

DI

Dependency Injection. The maintenance of dependencies is called dependency injection. The data that can be injected falls into three categories:

  • Basic types and Strings.
  • Complex type/collection type.
  • Other Bean types (configured beans in configuration files or annotations).

There are three injection modes:

  • Provided using the constructor.
  • Provided using the set method.
  • Provided using annotations.

Commonly used annotations

Classification of annotations

Used to create objects

Same as the

tag in the XML configuration file.

  • @Component

    • Effect: Stores the current class object into the Spring container.
    • Properties:
      • Value Specifies the Bean ID, which defaults to the current class name (lowercase first letter).
  • @Controller

  • @Service

  • @Repository

    @Controller, @Service, and @Repository have exactly the same functions and properties as Component. Just the semantics are different. @Controller is used in the presentation layer, @Service is used in the business layer, and @Repository is used in the persistence layer.

  • @Bean

    • Function:

    The return value of the current method is stored as a Bean object in Spring’s IOC container.

    • Properties:
      • Value Specifies the Bean ID. The default is the current method name.

Used to inject data

Same as the tag in the

tag of the XML configuration file.

  • @Autowired
    • Function:

    Automatic injection by type. As long as there is a unique Bean object type in the container that matches the type of the variable being injected. The injection succeeds. An error will be reported if there are no beans to inject or if there are multiple beans.

  • @Qualifier
    • Function:

    Injection by name in addition to injection by type. Cannot be used for class member injection alone (it must be used with @AutoWired), can be used for method parameter injection.

    • Properties:
      • Value Specifies the ID of the injected Bean.
  • @Resource
    • Function:

    Inject directly by Bean ID and can be used separately.

    • Properties:
      • Name Specifies the ID of the injected Bean.

    The above three cannot inject primitives and strings. The same goes for collection types (which can only be injected through XML).

  • @Value
    • Function:

    Inject primitive and String data.

    • Properties:
      • Value Specifies the value of the data that can be SpEL expressions (${expression}).

Used to change the scope of action

Same as the scope attribute in the

tag of the XML configuration file.

  • @Scope
    • Function:

    Specify the scope of the Bean.

    • Properties:
      • Value Specifies the value of a range (singleton, prototype, etc.).

Life cycle related

As with the init-method and destroy-method properties in the

tag of the XML configuration file.

  • @PreConstruct
    • Function:

    Specify the initialization method.

Configuration related

  • @Configuration

    • Function:

    Specifies that the current class is a configuration class.

    • Details:

    When the parameters of the configuration class as AnnotationConfigApplicationContext object creation, annotations can not write.

  • @ComponentScan

    • Function:

    Specifies the packages that Spring scans when creating the container.

    • Properties:
      • BasePackage package name.
  • @Import

    • Function:

    Used to import other configuration classes.

    • Properties:
      • Value specifies the bytecode of other configuration classes. The imported configuration classes are subconfiguration classes.
  • @PropertySource

    • Function:

    Used to specify the location of the properties file.

    • Properties:
      • Value Specifies the name and path of the file (using classpath for under classpath).
  • @EnableTransactionManagement

    • Function:

    Turn on Spring support for transactions.

AOP

Aspect Oriented Programming. Existing methods can be enhanced while the program is running without modifying the source code (it is also possible to extract duplicate but less relevant code while coding). The decorators in JavaScript and Python are AOP, and Java’s dynamic proxies are AOP ideas.

Spring implements AOP annotations

  • @Aspect

    • Function:

    Indicates that the current class is a faceted class.

  • @Before

    • Function:

    Indicates that this is the pre-notification method.

  • @AfterReturning

    • Function:

    Indicates that this is a post-notification method.

  • @AfterThrowing

    • Function:

    Indicates this is an exception notification method.

  • @After

    • Function:

    Indicates that this is the final notification method.

  • @Around

    • Function:

    Indicates that this is the surround notification method.

  • @Pointcut

    • Function:

    Indicates that this is a pointcut expression.

Spring Transaction Control

Transaction isolation level

  • ISOLATION_DEFAULT
    • Default level (whatever the database level is).
  • ISOLATION_READ_UNCOMMITTED
    • Uncommitted data can be read.
  • ISOLATION_READ_COMMITTED
    • Only submitted data can be read to solve the dirty read problem. Oracle default level.
  • ISOLATION_REPEATABLE_READ
    • Whether to read the modified data committed by other transactions, resolving the problem of unrepeatable reads (MySQL default level).
  • ISOLATION_SERIALIZABLE
    • Whether to read data added by other transaction commits, resolve the phantom read problem.

Propagation behavior of transactions (common)

  • REQUIRED
    • If there is no transaction, a new one is created (default).
  • SUPPORTS
    • Current transactions are supported, and if there are no current transactions, they are executed nontransactionally.

timeout

The default value is -1, with no timeout limit. If so, it is measured in seconds.

TransactionStatus

Information about the state of the transaction object at a point in time, including six specific operations:

  • Refresh transactions.
    • void flush()
  • Gets whether a memory point exists.
    • boolean hasSavepoint()
  • Gets whether the transaction is complete.
    • boolean isCompleted()
  • Gets whether the transaction is a new transaction.
    • boolean isNewTransaction()
  • Gets whether the transaction is rolled back.
    • boolean isRollbackOnly()
  • Set transaction rollback.
    • void setRollbackOnly()

Use (notes)

Add @Transactional to classes that require transactions.

@ Transactional (propagation = propagation. The SUPPORTS, readOnly = true)Service Implementation Class (){}Copy the code