An article takes you to learn Spring from entry to mastery

1. Overview of the Spring framework

1. Introduction to Spring

Spring is the most popular enterprise-level Java application development framework, used by millions of developers around the world to create performant, easily testable, reusable code

The Spring Framework is an open source Java platform originally written by Rod Johnson and first released under the Apache 2.0 license in June 2003

Spring is a lightweight framework, its base version is only about 2 MB in size

The core features of the Spring framework can be used to develop any Java application, but building web applications on the Java EE platform requires extensions. The goal of the Spring Framework is to make J2EE development easier, promoting good programming practices by enabling a POJO-based programming model

2. Spring three-tier architecture

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-WWpFkRFy-1677231115454)(note picture/image-20230208173447318.png)]

3. Advantages of Spring

4. Technical advantages of Spring

• **Non-intrusive:** Objects in applications developed based on Spring may not depend on Spring's API

•**Inversion of Control:**IOC——Inversion of Control, refers to the right to create objects to Spring to create. Before using Spring, objects were created by ourselves in the code new. And after using Spring. Object creation is given to the Spring framework.

•**Dependency Injection: **DI——Dependency Injection means that the dependent object does not need to manually call the setXX method to set, but is assigned through configuration.

•**Aspect Oriented Programming:**Aspect Oriented Programming——AOP

•**Container:**Spring is a container because it contains and manages the life cycle of application objects

• **Componentization: **Spring implements the combination of simple component configurations into a complex application. These objects can be combined in Spring using XML and Java annotations.

•**One-stop:**On the basis of IOC and AOP, open source frameworks and excellent third-party libraries for various enterprise applications can be integrated (in fact, Spring itself also provides SpringMVC for the presentation layer and Spring JDBC for the persistence layer )

5. Spring key concepts

Dependency Injection (DI):

The technique most recognized by Spring is the Dependency Injection (DI) pattern of Inversion of Control. Inversion of Control (IoC) is a general concept that can be expressed in many different ways, dependency injection is just one specific example of Inversion of Control.

When writing a complex Java application, the application classes should be as independent as possible from other Java classes to increase the possibility of reusability of these classes, and when unit testing, they can be tested independently of other classes. Dependency injection (or wiring as it is sometimes called) helps glue these classes together and at the same time keep them independent.

Aspect Oriented Programming (AOP):

A key component of the Spring Framework is the aspect-oriented programming (AOP) framework. Functionality that spans multiple points in a program is known as crosscutting concerns, which are conceptually independent of the application's business logic. There are various common good examples of aspects such as logging, declarative transactions, security, and caching to name a few.

The key unit of modularity in OOP is the class, and the key unit of modularity in AOP is the aspect. AOP helps you separate cross-cutting concerns from the objects they affect, whereas dependency injection helps you separate your application objects from each other.

2. Spring architecture

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-pYBAIr4r-1677231115456)(note picture/image-20230208173728424.png)]

1. Core container

  1. The spring-core module provides the basic building blocks of the framework, including IoC and dependency injection capabilities.
  2. The spring-beans module provides BeanFactory, a subtle implementation of the factory pattern that removes the need for coded singletons and decouples configuration and dependencies from the actual coded logic.
  3. The context module builds on the core and beans modules. The Context module inherits from the Bean module and adds internationalization (eg, using resource bundles), event propagation, resource loading, and transparent creation of contexts (eg, via Servlet container) and other functions.
  4. The spring-expression module provides a powerful expression language for querying and manipulating object graphs at runtime. It is an extension of the unified expression language defined in the JSP2.1 specification. It supports set and get attribute values, attribute assignment, method invocation, access to the contents of array collections and indexes, logical arithmetic operations, named variables, and access from the Spring IoC container by name. Retrieve objects, and also support projection, selection, and aggregation of lists.

2. Data Access - Integration

JDBC module : Provides a JDBC abstraction layer that eliminates tedious JDBC coding and parsing of database vendor-specific error codes.

ORM module : Provides integration with popular object-relational mapping APIs, including JPA, JDO, and Hibernate. Through this module, these ORM frameworks can be integrated with other functions of spring, such as the transaction management mentioned above.

OXM module : Provides support for OXM implementations, such as JAXB, Castor, XML Beans, JiBX, XStream, etc.

JMS module : Contains functions for producing and consuming messages. Starting from Spring 4.1, the spring-messaging module is integrated.

Transaction module : support programmatic and declarative transaction management for implementing special interface classes and all POJOs

3. Web application service

Web module : Provides basic web-oriented functions and web-oriented application contexts, such as multipart (multipart) file upload function, using Servlet listener to initialize IoC container, etc. It also includes the HTTP client and the web-related parts of Spring Remoting.

Web-MVC module : Provides implementations of Model View Control (MVC) and REST Web services for web applications. Spring's MVC framework can completely separate the domain model code from the web form, and can be integrated with all other functions of the Spring framework.

• **Web-Socket module: ** provides support for WebSocket-based, and provides two ways of communication between client and server in web applications.

• **Web-Portlet module: ** Provides an MVC implementation for the Portlet environment and reflects the functionality of the spring-webmvc module.

4. Other modules

AOP module : Provides an aspect-oriented (aspect) programming implementation, allowing you to define method interceptors and entry points to cleanly decouple the code, so that the code that implements the function is completely decoupled. Using source-level metadata, behavioral information can be incorporated into code in a manner similar to .Net properties.

Aspects module : Provides integration with AspectJ, a powerful and mature aspect-oriented programming (AOP) framework.

Instrumentation module : Provides the support of class instrumentation and the implementation of class loader in certain application servers.

•**Messaging Module:** Provides support for STOMP as a WebSocket sub-protocol for use in applications. It also supports an annotated programming model for routing and handling STOMP messages from WebSocket clients.

•**Test Module:** Supports testing of Spring components with JUnit or TestNG frameworks.

3. Build the Spring project

1. Idea building steps

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-vfT3FzLB-1677231115457)(note picture/image-20230208162907671.png)]

2. Introduce Spring dependencies

<!-- spring核心包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.13</version>
        </dependency>

<!-- junt单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>

3. Test the spring container

Create a spring.xml configuration file in the resource directory

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- User类的对象就交给Spring管理了 -->
    <bean id="user" class="com.huawei.SpringDemo2023.User" />

</beans>

Create a test class in the text/java directory for testing

//单元测试注解,代表可以直接运行该方法测试,需要依赖Junit类库
    @Test
    public void test1(){
    
    

        /**
         * 测试从Spring容器中取得指定的实例
         * 1.加载Spring配置文件
         * 2.取出Bean容器中的实例
         * 3.调用类中的方法
         */
        //1.加载Spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        //2.取出Bean容器中的实例
        User user = (User)context.getBean("user");
        //3.调用方法
        user.say();

    }

4. Spring-IOC container

1. What is an ioc container?

Introduction :

The Spring container is at the heart of the Spring framework. The container will create objects, wire them together, configure them, and manage their entire lifecycle from creation to destruction. The Spring container uses dependency injection (DI) to manage the components that make up an application. These objects are called Spring Beans

Technical features:

The IOC container is a container with a dependency injection function, which can create objects. The IOC container is responsible for instantiating, locating, and configuring objects in the application and establishing dependencies between these objects. Usually a new instance is controlled by the programmer, and "inversion of control" means that the work of the new instance is not done by the programmer but by the Spring container. BeanFactory is the actual representative of the IOC container in Spring.

2. Two IOC containers provided by Spring

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-1Es1gFSD-1677231115458)(note picture/image-20230208174226616.png)]

Notice:

The ApplicationContext container includes all the functionality of the BeanFactory container, so it is generally not recommended to use the BeanFactory. BeanFactory can still be used in lightweight applications, such as mobile devices or applet-based applications, where its data volume and speed are significant.

1. BeanFactroy container

Overview:

This is the simplest container, and its main function is to provide support for dependency injection (DI). This container interface is defined in org.springframework.beans.factory.BeanFactory.

In Spring, there are a large number of implementations of the BeanFactory interface. Among them, the most commonly used is the XmlBeanFactory class. The container reads configuration metadata from an XML file and generates a configured system or application from these metadata.

Example of how to load container configuration:

/**
         * BeanFactory创建实例步骤
         * 1.创建工厂类,加载Bean.xml配置文件
         * 2.通过工厂的方法获取指定对象实例
         * 3.实例对象调用方法
         */
        //1.创建工厂类,加载Bean.xml配置文件
        XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("Bean.xml"));
        //2.通过工厂的方法获取指定对象实例
        User user = (User) factory.getBean("user");
        //3.实例对象调用方法
        user.say();

2. Spring ApplicationContext container

Overview:

Application Context is a subinterface of BeanFactory, also known as Spring context.

Application Context is a higher-level container in spring. Similar to BeanFactory, it can load the beans defined in the configuration file, gather all the beans together, and allocate beans when there is a request. In addition, it adds the functions required by the enterprise. ApplicationContext contains all the functions of BeanFactory. Generally speaking, ApplicationContext will be better than BeanFactory.

The Applicationcontext container provides three implementations of loading the ioc container :

ClassPathXmlApplicationContext : This container loads defined beans from an XML file. Here, you don't need to provide the full path of the XML file, just configure the CLASSPATH environment variable correctly, because the container will search the bean configuration file from the CLASSPATH.

FileSystemXmlApplicationContext : This container loads defined beans from an XML file. Here, you need to provide the full path to the constructor XML file .

WebXmlApplicationContext : This container loads beans defined in XML files within the scope of a web application.

1. ClassPathXmlApplicationContext loads the container

//1. 加载配置文件,初始化容器
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        //2. 通过同期的getBean方法获取容器中创建的对象
        User user = (User) context.getBean("user");
        System.out.println("2. 从容器中获取对象");
        user.say();

2. FileSystemXmlApplicationContext loading container

/**
*第一步生成工厂对象。加载完指定路径下 bean 配置文件后,利用框架提供的 FileSystemXmlApplicationContext *API 去生成工厂 bean
*/
ApplicationContext context = new FileSystemXmlApplicationContext
            ("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml");
//第二步利用第一步生成的上下文中的 getBean() 方法得到所需要的 bean。 这个方法通过配置文件中的 bean ID 来返回一个真正的对象。
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
//来调用任何方法
      obj.getMessage();

3. The scope of beans

When defining a bean in Spring, you must declare options for the bean's scope.

The Spring framework supports the following five scopes: singleton, prototype , request, session and global session

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-j5GZpPdK-1677231115459)(note picture/image-20230209104632122.png)]

Spring configuration file configuration method

<bean id="user" scope="prototype" class="com.huawei.SpringDemo2023.User" />

4. Bean life cycle

Bean life cycle can be expressed as: Bean definition - Bean initialization - Bean use - Bean destruction

1. Lazy loading of beans

When lazy-init=true is configured, it means that the bean is in lazy loading mode, and when the bean is used, it will be initialized.

It has two values: true, false (default) true means that the bean is lazy loaded, and the container is not initialized when it is initialized.

<bean id="user" lazy-init="false" class="com.huawei.SpringDemo2023.User" />

2. bean's init-method method (initialization callback) and destroy-method method (destruction callback)

1. Initialize the init-method method

Implementation 1: Let the class implement the InitializingBean interface and rewrite the afterPropertiesSet method

In the spring.xml configuration file, the corresponding bean tag does not need to configure the init-method attribute

bean class

public class User implements InitializingBean {
    
    

    private String name;

    public User(){
    
    
        System.out.println("我被实例了!");
    }

    public void say(){
    
    
        System.out.println("我被调用了!");
    }

    /**
     * 初始化回调-在对象创建完成之后
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
    
    
        System.out.println("init初始化-afterPropertiesSet()");
    }
}

test

//1. 加载配置文件,初始化容器
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

Implementation 2 (recommended): configure the init-method method in the bean tag

Non-intrusive configuration method

In the spring configuration file

<bean id="user" init-method="userInit"  class="com.huawei.SpringDemo2023.User" />

Define the userInit method in the bean class

public class User{
    
    

    private String name;

    public User(){
    
    
        System.out.println("我被实例了!");
    }

    public void say(){
    
    
        System.out.println("我被调用了!");
    }

    /**
     * 初始化回调-在对象创建完成之后
     * @throws Exception
     */
    public void userInit(){
    
    
        System.out.println("userInit调用!");
    }
}

2. bean destruction callback -destroy method

Implementation 1: Implement the DisposableBean interface and rewrite the destroy method
public class User implements DisposableBean {
    
    

    private String name;

    public User(){
    
    
        System.out.println("我被实例了!");
    }

    public void say(){
    
    
        System.out.println("我被调用了!");
    }

    /**
     * 当对象销毁时回到
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
    
    
        System.out.println("destroy-调用");
    }
}
Implementation 2: Add in the configuration file
<bean id="user" destroy-method="userDes" class="com.huawei.SpringDemo2023.User" />

bean class

public class User{
    
    

    private String name;

    public User(){
    
    
        System.out.println("我被实例了!");
    }

    public void say(){
    
    
        System.out.println("我被调用了!");
    }

    /**
     * 当对象销毁时回到
     * @throws Exception
     */
    public void userDes(){
    
    
        System.out.println("userDes-调用");
    }
}

test

//1. 加载配置文件,初始化容器
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        // registerShutdownHook 方法是关闭所有bean,触发销毁
        context.registerShutdownHook();

3. Pre- and post-processor classes

effect:

​ When the init-method method called by the bean in the container is called back, add operations before and after calling the method

Implementation process

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-n9tGO5K8-1677231115460) (note picture/image-20230209140256399.png)]

Create a pre and post processor class

package com.huawei.SpringDemo2023;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * 前后置处理器类
 * 2023/2/9
 */
public class MyBeanPostProcessor implements BeanPostProcessor {
    
    

    /**
     * 前置处理器
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    
    
        System.out.println("-----------1.前置处理器--------------");
        System.out.println("bean:"+bean);
        System.out.println("beanName:"+bean);

        return bean;
    }

    /**
     * 后置处理器
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    
    
        System.out.println("-----------2.后置处理器--------------");
        System.out.println("bean:"+bean);
        System.out.println("beanName:"+bean);
        return bean;
    }

}

Configure instances of pre- and post-processor classes in spring

 <!-- User类的对象就交给Spring管理了 -->
    <bean id="user" init-method="userInit" class="com.huawei.SpringDemo2023.User" />
    <bean id="stu" init-method="initStu" class="com.huawei.SpringDemo2023.Student" />

    <!--    前后置处理器类 -->
    <bean id="myBeanPostProcessor" class="com.huawei.SpringDemo2023.MyBeanPostProcessor" />

Note: The triggering of the pre- and post-processors will be triggered even if the init method is not defined, and the timing becomes before and after calling the constructor

If there are multiple implementations of pre- and post-processor classes, implement the Ordered interface and implement the getOrder method

1. After rewriting the getOrder method, the smaller the return value, the higher the execution priority

2. The execution priority is the same. According to the Spring configuration file, the order in which beans are configured is the priority.

/**
 * 前后置处理器类
 * 2023/2/9
 */
public class MyBeanPostProcessor implements BeanPostProcessor, Ordered {
    
    

    /**
     * 前置处理器
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    
    
        System.out.println("-----------1.MyBeanPostProcessor-前置处理器--------------");

        return bean;
    }

    /**
     * 后置处理器
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    
    
        System.out.println("-----------2.MyBeanPostProcessor-后置处理器--------------");
        return bean;
    }

    /**
     * 执行先后的顺序 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     * @return
     */
    @Override
    public int getOrder() {
    
    
        return 800;// 值越小执行优先级越高
    }
}

4. Injection of bean properties

Call the set method of the bean class to inject properties

<!-- User类的对象就交给Spring管理了 -->
    <bean id="user" class="com.huawei.SpringDemo2023.User" >
        <property name="name" value="cxk" />
    </bean>
public class User{
    
    

    private String name;

    public User(){
    
    
        System.out.println("我被实例了!");
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        System.out.println("user-set方法调用-value:"+name);
        this.name = name;
    }
}

5. bean definition inheritance

    <!-- 定义一个抽象模板 -->
    <bean id="templateA" abstract="true">
        <property name="name" value="123"></property>
    </bean>

    <!-- bean类通过parent属性继承  -->
    <bean id="user" class="com.huawei.SpringDemo2023.User" parent="templateA" >
		<!-- <property name="name" value="cxk" />-->
    </bean>

5. Spring-Dependency Injection

1. The method injected by the constructor

the injected class

public class TestEditor {
    
    
	// 需要spring注入的对象
    private SpellChecker spellChecker;

    public TestEditor(){
    
    
        System.out.println("TestEditor-实例化:无参构造器");
    }
	//通过有参构造器将要注入的对象创建的责任抛出(给springioc容器来操作)
    public TestEditor(SpellChecker spellChecker) {
    
    
        System.out.println("TestEditor-实例化:"+spellChecker);
        this.spellChecker = spellChecker;
    }

    public void testDI(){
    
    
        this.spellChecker.aa();
    }

}

class to inject

public class SpellChecker {
    
    

    public void aa(){
    
    
        System.out.println("SpellChecker-AA");
    }

}

Spring Configuration - Constructor Injection

<!-- 准备需要注入对象的实例 -->
   <bean id="spellChecker" class="com.huawei.SpringDemo2023.SpellChecker" />

    <!-- 要注入实例的类 -->
    <bean id="testEditor" class="com.huawei.SpringDemo2023.TestEditor">
        <!-- 将要注入的实例放入TestEditor类的有参构造器的第一个参数,如果有多个参数根据标签顺序注入 -->
        <!-- 当TestEditor加载时就会自动调用有参构造器,进行对象注入 -->
        <constructor-arg ref="spellChecker" />
    </bean>

1. Constructor basic data type or String type injection method

If you want to pass a reference to an object, you need to use the ref attribute of the tag, and if you want to pass the value directly, then you should use the value attribute as shown above.

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-0k3jVP7n-1677231115462)(note picture/image-20230209150739808.png)]

The type is distinguished by the type attribute, the basic data type directly writes the name, and the reference type writes the fully qualified name

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-651Yzf0q-1677231115463)(note picture/image-20230209150742124.png)]

2. The index attribute value controls the order of injected parameters

The index value starts from 0, such as: 0 represents the first parameter

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-PTn9oCAS-1677231115464)(note picture/image-20230209150916930.png)]

2. Injection via setter method

configuration file

<!-- 准备需要注入对象的实例 -->
   <bean id="spellChecker" class="com.huawei.SpringDemo2023.SpellChecker" />

    <!-- 要注入实例的类 -->
    <bean id="testEditor" class="com.huawei.SpringDemo2023.TestEditor">
        <!-- 调用了spellChecker对应的setter方法实现注入 -->
       <property name="spellChecker" ref="spellChecker" />
    </bean>

class to inject

public class TestEditor {
    
    

    private SpellChecker spellChecker;

    public SpellChecker getSpellChecker() {
    
    
        return spellChecker;
    }

    //setter方法就是提供给外部实现参数注入的方法
    public void setSpellChecker(SpellChecker spellChecker) {
    
    
        System.out.println("setter方法调用!");
        this.spellChecker = spellChecker;
    }

    public void testDI(){
    
    
        this.spellChecker.aa();
    }

}

3.p-namespace method injection

Comparison of old and new ways

The old way:

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-nuKE5M8w-1677231115464)(note picture/image-20230209164450002.png)]

new method:

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-oqNK2KDN-1677231115466)(note picture/image-20230209164452735.png)]

<!-- namespace方法会根据被注入的类自动调用构造器注入或setter方法注入 -->
<bean id="testEditor" class="com.huawei.SpringDemo2023.TestEditor"
   p:spellChecker-ref="spellChecker"
/>

4. Internal bean injection

The injection method of the bean tag configuration instance is written when the instance needs to be injected

Setter method internal bean injection

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-2HvOYqxn-1677231115467)(note picture/image-20230209164615169.png)]

Bean injection inside the constructor

<bean id="testEditor" class="com.huawei.SpringDemo2023.TestEditor" >
    <constructor-arg name="cxk">
        <bean class="com.huawei.SpringDemo2023.SpellChecker" />
    </constructor-arg>
</bean>

5. Collection injection

Collection injection is similar to inner bean injection

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-rpRYX0fp-1677231115467)(note picture/image-20230209164807138.png)]

Four types of injection cases:

the injected class

1. list injection

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-pbbxKxgJ-1677231115468)(note picture/image-20230209164917626.png)]

2. Set injection

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-Yk9B4awz-1677231115469)(note picture/image-20230209164920792.png)]

3.Map injection

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-RTWpt5Dh-1677231115470)(note picture/image-20230209164924396.png)]

4.Properties injection

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-ci1IydTr-1677231115470)(note picture/image-20230209164928374.png)]

5. Mixed data injection in collection (instance injection collection)

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-rbSKhhly-1677231115471)(note picture/image-20230209165001312.png)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-dMsA3sLO-1677231115472)(note picture/image-20230209165004041.png)]

6. Automatic assembly of SpringBean

The Spring container can autowire relationships between cooperating beans without using and elements, which helps reduce the amount of XML configuration that is written for a large Spring-based application.

1. Automatic assembly mode

The following autowiring patterns, which can be used to instruct the Spring container to use autowiring for dependency injection. You can specify the autowire mode for a bean definition using the element's autowire attribute.

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-hxCS8XSv-1677231115472)(note picture/image-20230209165048732.png)]

1.1 Assembly mode-byName

This mode is specified by property name for autowiring. The Spring container is seen as beans whose auto-wire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties to beans of the same name defined in the configuration file. If it finds a match, it will inject those beans, otherwise, it will throw an exception.

class to inject

public class TestEditor {
    
    

    private SpellChecker cxk;

    public SpellChecker getCxk() {
    
    
        return cxk;
    }
    
    // byName方法只会使用setter方法实现注入
    public void setCxk(SpellChecker cxk) {
    
    
        this.cxk = cxk;
    }
}

configuration

<!-- 准备需要注入对象的实例 -->
<bean id="cxk" class="com.huawei.SpringDemo2023.SpellChecker" />

<bean id="testEditor" class="com.huawei.SpringDemo2023.TestEditor" autowire="byName"></bean>

1.2 Assembly mode-byType

This mode is specified by property name for autowiring. The Spring container is seen as beans whose autowire attribute is set to byType in the XML configuration file. It then tries to match and wire its properties to beans of the same name defined in the configuration file. If it finds a match, it will inject those beans, otherwise, it will throw an exception.

class to inject

public class TestEditor {
    
    

    private SpellChecker cxk;

    public SpellChecker getCxk() {
    
    
        return cxk;
    }

    // byType方法只会使用setter方法实现注入
    public void setCxk(SpellChecker cxk) {
    
    
        System.out.println("setter");
        this.cxk = cxk;
    }
}

configuration

!-- 准备需要注入对象的实例 -->
<bean id="xxx" class="com.huawei.SpringDemo2023.SpellChecker" />

<bean id="testEditor" class="com.huawei.SpringDemo2023.TestEditor" autowire="byType"></bean>

1.3 Assembly mode-constructor

This pattern is very similar to byType, but it applies to constructor parameters. The Spring container is seen as beans whose autowire attribute is set to constructor in the XML configuration file. It then tries to match and wire its constructor's parameter with one of the beans names in the configuration file. If it finds a match, it injects those beans, otherwise, it throws an exception.

class to inject

public class TestEditor {
    
    

    private SpellChecker cxk;

    // constructor方法只会使用构造器方法实现注入
    public TestEditor(SpellChecker cxk) {
    
    
        System.out.println("构造器");
        this.cxk = cxk;
    }
}

configuration

<!-- 准备需要注入对象的实例 -->
    <bean id="xxx" class="com.huawei.SpringDemo2023.SpellChecker" />

    <bean id="testEditor" class="com.huawei.SpringDemo2023.TestEditor" autowire="constructor"></bean>

7. Spring annotation-based configuration

Purpose: Reduce complex configuration in xml

1. How to use - introduce annotations

<!-- 将所有Spring关于依赖注入的注解类实例化存放到ioc容器汇总 -->
    <context:annotation-config />

2. Common annotations

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-UhGdPsgu-1677231115473)(note picture/image-20230209171124594.png)]

1. @Required annotation

Required validation annotation, @Required annotation is applied to the setter method of the bean property, which indicates that the affected bean property must be placed in the XML configuration file at configuration time, otherwise the container will throw a BeanInitializationException.

Spring version 5.3.9, annotations are deprecated, if not filled, no error will be reported

class to inject

public class TestEditor {
    
    

    private SpellChecker cxk;

    
    public SpellChecker getCxk() {
    
    
        return cxk;
    }
    
    // 只能用于setter方法上
    @Required
    public void setCxk(SpellChecker cxk) {
    
    
        this.cxk = cxk;
    }
}

2. @Autowired annotation

The @Autowired annotation provides more fine-grained control over where and how autowiring is done.

The @Autowired annotation can be used to autowire beans in setter methods, like @Autowired annotations, containers, a property or arbitrarily named methods that may take multiple parameters.

Note: 1. This annotation has a function similar to the @Required annotation by default, and there is a required verification

2. In the class to be injected, provide a parameterized constructor as much as possible to provide a parameterless constructor, because the spring instance object needs to call the constructor, in case the constructor must pass a value and spring has no instance to inject, it will throw UnsatisfiedDependencyException

@Autowired (required=false) non-required verification

1. Use on the setter method

// 使用setter方法注入,装配方法根据byType规则装配
    @Autowired
    public void setCxk(SpellChecker cxk) {
    
    
        System.out.println("setter:"+cxk);
        this.cxk = cxk;
    }

2. Use on attributes

You can use @Autowired annotation on properties to remove setter methods. When using automatic connection attribute transfer, Spring will automatically assign these passed values ​​​​or references to those attributes.

// 如果有构造器,则采用构造器的方式进行注入,如果没有则直接根据类型注入到属性中不调用setter方法
// 装配模式依旧使用bytype方法匹配
@Autowired
private SpellChecker cxk;

3. Use on the constructor

You can also use @Autowired in the constructor. A constructor @Autowired indicates that when the bean is created, the constructor will be automatically wired even if the bean is not configured with an element in the XML file.

// 注入方式使用构造器注入,匹配规则使用byType方法匹配
@Autowired
public TestEditor(SpellChecker cxk) {
    
    
    System.out.println("构造器:"+cxk);
    this.cxk = cxk;
}

3. Qualifier annotation - with @Autowired annotation

Role: @Autowired annotation autowiring is based on the selection injection through the bean's id or name

spring configuration file

<bean id="textEditor" class="com.huawei.SpringDemo2023.TestEditor"></bean>

    <bean id="j1" class="com.huawei.SpringDemo2023.SpellChecker" >
        <property name="name" value="高启强" />
    </bean>

    <bean id="j2" class="com.huawei.SpringDemo2023.SpellChecker" >
        <property name="name" value="高启盛" />
    </bean>

class to inject

public class TestEditor {
    
    

    @Autowired // 在byType的基础上选择
    @Qualifier("j1")// 选择bean的name或id
    private SpellChecker cxk;


    public void test(){
    
    
        System.out.println(this.cxk.getName());
    }

}

4. Spring-JSR250 annotations

1.@PostConstruct@PreDestroy

public class TestEditor {
    
    

    private SpellChecker cxk;

    /**
     * 替代了init-method配置,描述初始化回调方法
     */
    @PostConstruct
    public void testInit(){
    
    
        System.out.println("初始化!");
    }

    /**
     * 替代了destroy-method配置,描述销毁回调方法
     */
    @PreDestroy
    public void testDestory(){
    
    
        System.out.println("销毁!");
    }

}

2. @Resource annotation

1. Name attribute definition

Wiring assembly by bean id or name

<!-- 将所有Spring关于依赖注入的注解类实例化存放到ioc容器汇总 -->
<context:annotation-config />

<bean id="textEditor" class="com.huawei.SpringDemo2023.TestEditor"></bean>

<bean id="j1" class="com.huawei.SpringDemo2023.SpellChecker" >
    <property name="name" value="高启盛" />
</bean>
public class TestEditor {
    
    

    @Resource(name="j1")
    private SpellChecker cxk;

    public TestEditor(){
    
    
        System.out.println("构造器:无参");
    }

    public TestEditor(SpellChecker cxk){
    
    
        System.out.println("构造器:有参"+cxk);
        this.cxk = cxk;
    }

    public SpellChecker getCxk() {
    
    
        return cxk;
    }

    public void setCxk(SpellChecker cxk) {
    
    
        System.out.println("setter");
        this.cxk = cxk;
    }
}

2.Type attribute definition

Autowiring by type

public class TestEditor {
    
    

    // 注入的实例类型是SpellChecker类型以及子类
    @Resource(type = SpellChecker.class)
    private SpellChecker cxk;

    public TestEditor(){
    
    
        System.out.println("构造器:无参");
    }

    public TestEditor(SpellChecker cxk){
    
    
        System.out.println("构造器:有参"+cxk);
        this.cxk = cxk;
    }

    public SpellChecker getCxk() {
    
    
        return cxk;
    }

    public void setCxk(SpellChecker cxk) {
    
    
        System.out.println("setter");
        this.cxk = cxk;
    }
}
3. Two attributes can be used at the same time

First distinguish by type, then connect by name

@Resource(name = "j1",type = SpellChecker.class)
private SpellChecker cxk;

5. @Configuration and @Bean annotations

@Configuration: Identifies that this is a class that configures a bean instance ( on the class declaration )

@Bean: Identifies a method that returns an instance object ( on the method declaration )

Role: Use classes and methods to replace the way xml configures containers

Note: Using the method configuration of the class, you can load the instance without writing the bean tag in the spring.xml file

configuration class

package com.huawei.SpringDemo2023;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 容器配置类
 * 2023/2/10
 */
// 描述当前类是一个描述ioc容器配置的类---》等同于spring.xml配置文件
    // 想要获取当前类中的bean配置需要加载当前配置类
@Configuration
public class MyConfig {
    
    

    // <bean id="getSpellCheckerJ1" class="om.huawei.SpringDemo2023.SpellChecker" />
    // 被添加@Bean注解的方法返回值实例将添加到ioc容器中
    @Bean
    public SpellChecker getSpellCheckerJ1(){
    
    
        SpellChecker spellChecker = new SpellChecker();
        spellChecker.setName("高启盛");
        return spellChecker;
    }

}

test

/**
 * 测试工具类
 * 2023/2/8
 */
public class TextClass {
    
    

    @Test
    public void text1(){
    
    

        //ApplicationContext
        System.out.println("1. 容器加载");
        //1. 加载配置类,初始化容器
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);

        //传统的方法通过名称获取,此时传递的是方法的名称
        SpellChecker bean = (SpellChecker) context.getBean("getSpellCheckerJ1");

        // 通过bean方法的返回值类型获取实例
        //SpellChecker bean = context.getBean(SpellChecker.class);

        System.out.println(bean.getName());

    }

}

Multi-configuration class registration method

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-0MUl6Sjr-1677231115474)(note picture/image-20230210112309264.png)]

6. @Import annotation

@import annotation, import the content of another configuration class to merge

Note: This annotation can only be used on configuration class declarations

configuration class-1

@Configuration
@Import(MyConfig2.class)// config2导入到当前配置类
public class MyConfig {
    
    

    // <bean id="getSpellCheckerJ1" class="om.huawei.SpringDemo2023.SpellChecker" />
    // 被添加@Bean注解的方法返回值实例将添加到ioc容器中
    @Bean
    public SpellChecker getSpellCheckerJ1(){
    
    
        SpellChecker spellChecker = new SpellChecker();
        spellChecker.setName("高启盛");
        return spellChecker;
    }

}

configuration class-2

@Configuration
public class MyConfig2 {
    
    

    @Bean
    public SpellChecker getSpellCheckerJ2(){
    
    
        SpellChecker spellChecker = new SpellChecker();
        spellChecker.setName("蔡徐坤");
        return spellChecker;
    }

}

test

@Test
public void text1(){
    
    

    //ApplicationContext
    System.out.println("1. 容器加载");
    //1. 加载配置类,初始化容器
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);

    //传统的方法通过名称获取,此时传递的是方法的名称
    SpellChecker bean = (SpellChecker) context.getBean("getSpellCheckerJ2");

    // 通过bean方法的返回值类型获取实例
    //SpellChecker bean = context.getBean(SpellChecker.class);

    System.out.println(bean.getName());

}

7. @Bean can configure the declaration cycle function

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-FBNye9JH-1677231115474)(note picture/image-20230210113036240.png)]

8. @Scope annotation-Bean scope annotation

The default scope is single instance,

But you can override this method with @Scope annotation

@Scope("prototype") multi-instance, when the IOC container starts to create, it will not create an object and put it in the container. When you need it, you need to get the object from the container, and it will be created.

@Scope("singleton") When the single-instance IOC container starts, it will call the method to create the object, and then every time it gets the same object from the container (in the map).

@Scope("request") creates an instance with the same request

@Scope("session") creates an instance with the same session

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-mCt8gqxX-1677231115475)(note picture/image-20230210113350539.png)]

8.Spring-AOP

There is the same business code in each class, which can be solved by aop

AOP (Aspect Orient Programming), literal translation is aspect-oriented programming. AOP is a programming idea that is a complement to object-oriented programming (OOP). Object-oriented programming abstracts the program into objects at various levels, while aspect-oriented programming abstracts the program into various aspects.

Why use AOP?

Imagine the following scenario, there is a certain piece of repeated code between multiple modules during development, how do we usually deal with it? Obviously, no one will rely on "copy and paste". In traditional process-oriented programming, we also abstract this code into a method, and then call this method where needed, so that when this code needs to be modified, we only need to change this method. However, the requirements are always changing. One day, a new requirement is added, and more modifications need to be made. We need to abstract another method, and then call this method separately where needed, or we don’t need this method anymore. , we still have to delete every place where this method is called. In fact, we can use AOP to solve problems that involve the same modification in multiple places.

AOP Terminology

Attribute terms in the AOP world:

Advice: Enhanced handling in the AOP framework. Advice describes when the aspect is executed and how to perform enhancement processing.

Join point: A join point represents a point in the process of application execution where an aspect can be inserted. This point can be a method call or an exception thrown. In Spring AOP, a join point is always a method invocation.

PointCut: Join points for enhanced processing can be inserted.

Aspect: An aspect is a combination of advice and pointcut.

Introduction: Introduction allows us to add new methods or properties to existing classes.

Weaving: Add enhancement processing to the target object and create an enhanced object. This process is weaving.

4. Use configuration AOP

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.8.RC3</version>
</dependency>

1. Realize AOP based on XML configuration method

1. Open aop configuration

<!-- AOP配置 -->
    <aop:config>
        ...
</aop:config>

2. Define a section

An aspect is an independent business logic of the entire aop, which needs to be described by a class

configuration

<aop:config>
    <!-- 描述一个切面(包含:连接点、通知的配置) -->
   <aop:aspect id="testAspect" ref="myAspect1"></aop:aspect>
</aop:config>

<!-- 配置切面的类 -->
<bean id="myAspect1" class="com.huawei.SpringDemo2023.aop.TestAspect" />

Noodles

package com.huawei.SpringDemo2023.aop;

/**
 * 描述切面功能类
 * 配置通知方法
 * 2023/2/10
 */
public class TestAspect {
    
    

   ... 

}

3. Define the cut point

The point of cut is to define when the aop operation will be triggered when which method or which class is accessed

Pointcut expression:

The method expression begins with the * sign, indicating that we don't care about the return value type of the method. Then we specified the fully qualified class name and method name. For the method parameter list, we use the ... markcut to select any play method, no matter what the input parameters of the method are. Between multiple matches, we can use link symbols &&, ||, ! To express the relationship of "and", "or" and "not". But when using XML file configuration, these symbols have special meanings, so we use "and", "or", "not" to represent.

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-8hWp8n2Y-1677231115481)(note picture/image-20230210150729527.png)]

Configuration example:

<!-- AOP配置 -->
    <aop:config>
        <!-- 描述一个切面(包含:连接点、通知的配置) -->
        <aop:aspect id="testAspect" ref="myAspect1">
            <!-- 描述一个切点 -->
            <aop:pointcut id="cut1" expression="execution(* com.huawei.SpringDemo2023.aop.Cat.catSay(..))" />
        </aop:aspect>
    </aop:config>

4. Define the notification

The function of notification: through aop, enhance the callback function of pre-call, post-post, return, and exception

Rules of use:

Pre-notification : called before the method is called, must be called

Post-notification : called after the method is called (called before the return value is returned), must be called

Notification after exception : Only the pointcut method exception will be triggered, and after an exception occurs, the notification will not be called after returning

Notification after return : Only when the pointcut method returns a normal value will it be triggered, if there is an exception, it will not be triggered

Surround notification : a method can implement pre-post, exception, and post-return notifications, control whether the target method is called, change the return value of the method, and apply to aop notifications in complex scenarios

​ Use surround notification, it is recommended not to use other notification types

Notification type:

notify Identify keywords describe
advance notice before Execution advice before a method is executed.
post notification after Advice is performed after a method has executed, regardless of its result.
Notify upon return after-returning After a method executes, advice can only be executed if the method completes successfully.
Notify when an exception is thrown after-throwing After a method executes, advice can only be executed if the method exits and throws an exception.
surround notification around Advice is performed before and after the advice method call.

The notification callback method belongs to the definition of the aspect and needs to be defined in the aspect class

Aspect class: define notification method

package com.huawei.SpringDemo2023.aop;

/**
 * 描述切面功能类
 * 配置通知方法
 * 2023/2/10
 */
public class TestAspect {
    
    

    /**
     * 前置通知
     */
    public void beforeAdvice(){
    
    
        System.out.println("----1.前置通知");
    }

    /**
     * 后置通知
     */
    public void afterAdvice(){
    
    
        System.out.println("----2.后置通知");
    }

}

Configure notifications (taking pre- and post-notifications as examples)

<!-- AOP配置 -->
    <aop:config>
        <!-- 描述一个切面(包含:连接点、通知的配置) -->
        <aop:aspect id="testAspect" ref="myAspect1">
            <!-- 描述一个切点 -->
            <aop:pointcut id="cut1" expression="execution(* com.huawei.SpringDemo2023.aop.Cat.catSay(..))" />
            <!-- 配置通知 -->
            <!-- 1.前置通知 -->
            <aop:before pointcut-ref="cut1" method="beforeAdvice" />
            <!-- 2.后置通知 -->
            <aop:after pointcut-ref="cut1" method="afterAdvice" />
        </aop:aspect>
    </aop:config>
1. Notification upon return

xml configuration

<!-- 3.返回后通知 returning 定义一个接收方法返回值的形参,如果方法没有返回值注入null,有返回值注入方法的返回值 -->
<aop:after-returning pointcut-ref="cut1" method="afterReturning" returning="retValue" />

Notification configuration in aspect class

/**
     * 返回后通知
     * retValue: 接收方法的返回值
     * 返回后通知只能得到切点方法的返回值,但是无法修改
     */
    public void afterReturning(Object retValue){
    
    
        System.out.println("---2.1 返回后通知");
    }
2. Notification after exception

xml configuration

<!-- 4.异常后通知 throwing 定义一个接收异常对象的形参 -->
            <aop:after-throwing pointcut-ref="cut1" method="afterThrowing" throwing="ex" />

Aspect configuration

/**
     * 异常后通知
     * ex: 切点方法异常抛出后,抛出的异常对象
     */
    public void afterThrowing(Throwable ex){
    
    
        System.out.println(ex);
        System.out.println("--2.2 异常后通知");
    }
3. Surround notifications

xml configuration

<!-- 配置通知 -->
<aop:around pointcut-ref="cut1" method="around" />

Surround advice method configured in the aspect class

public Object around(ProceedingJoinPoint point){
    
    

        System.out.println("---1.前置通知");
        try {
    
    
            Object obj = point.proceed();// 让切点方法执行,obj是切入点方法的返回值,如果没有返回值则返回null
            System.out.println("---2.1 返回后通知");
        } catch (Throwable e) {
    
    
            System.out.println("---2.2异常后通知");
        }
        System.out.println("---2.后置通知");

        return "哈哈哈哈";// 返回值返回的就是切点调用方法的返回值,可以通过自定义的方法改变返回值
    }

5. Call the method and trigger AOP

Defines the target class that the pointcut will enter

public class Cat {
    
    

    public void catSay(){
    
    
        System.out.println("喵!!!!");
    }

    public void catSay(String name){
    
    
        System.out.println("喵2!!!!"+name);
    }

}

Test whether aop will be triggered when cat's catSay method is called

At this point, the pointcut expression is: execution(* com.huawei.SpringDemo2023.aop.Cat.catSay(…))

 @Test
    public void text1(){
    
    

        //ApplicationContext
        System.out.println("1. 容器加载");
        //1. 加载配置类,初始化容器
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        Cat bean = context.getBean(Cat.class);

        bean.catSay();

    }

9. Automatic package class scanning based on annotation configuration

Reason for use:

In the previous examples, we used XML bean definitions to configure components. In a slightly larger project, there are usually hundreds of components. If these components are configured using XML bean definitions, it will obviously increase the size of the configuration file, and it is not convenient to find and maintain.

Spring2.5 introduces an automatic component scanning mechanism for us, which can look for classes marked with **@Component, @Service, @Controller, @Repository annotation** under the class path, and incorporate these classes into the Spring container for management .

1. How to configure and use

1. Enable automatic package scanning

<context:component-scan base-package="cn.itcast" />This configuration implicitly registers multiple processors for parsing and processing annotations, including context:annotation-config/processors registered by this configuration, that is, If you write <context:component-scan base-package="cn.itcast" /> configuration, you don’t need to write context:annotation-config/configuration, and base-package is the package (including subpackages) that needs to be scanned. After configuring this tag in xml, spring can automatically scan the java files under the base-pack or under the subpackage. If it scans classes with @Component @Controller@Service and other annotations, register these classes as beans

<!-- bean自动注入实例的扫描注解开启 -->
    <!-- 扫描com.huawei.SpringDemo2023的所有子包以及类带有@Component、@Service、@Controller、@Repository的类添加实例到ioc容器 -->
    <!-- 该配置自动将所有Spring关于依赖注入的注解都自动开启了 <context:annotation-config/> -->
    <!-- use-default-filters属性默认为true代表自动扫描报下所有带有@Component、@Service、@Controller、@Repository的类 -->
    <context:component-scan base-package="com.huawei.SpringDemo2023" />

2. Add annotations to the class declaration that needs to automatically create an instance and add it to the ioc container

Notes:

General common components: @Component

Controller layer class: @Controller

Business implementation layer class: @Service

case:

@Component
public class Cat {
    
    

    public void catSay(){
    
    
        System.out.println("喵!!!!");
    }


    public void catSay(String name){
    
    
        System.out.println("喵2!!!!"+name);
    }

}

get from the container

System.out.println("1. 容器加载");
        //1. 加载配置类,初始化容器
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        Cat bean = context.getBean(Cat.class);

        bean.catSay();

2. Specify (include-filter)/filter (exclude-filter) scan tag configuration

In addition, context:component-scan also provides two sub-tags (1) context:include-filter (2) context:exclude-filter Before explaining these two sub-tags, let’s talk about context:component-scan has a use-default- filters attribute, which defaults to true, which means that all classes marked with @Component under the specified package will be scanned, including sub-annotations @Service, @Repository, etc., and registered as beans

1. context:incluce-filter

It can be found that the granularity of this scan is too large. What should you do if you only want to scan the Controller under the specified package? At this point the subtag context:incluce-filter

Case implementation:

<context:component-scan base-package="com.huawei.*" use-default-filters="false">  
	<context:include-filter type="annotation"expression="org.springframework.stereotype.Controller"/>   
</context:component-scan> 

Notice:

Note here: If you add use-default-filters="false", it is true by default, and classes that are not annotated by @Controller may be scanned as beans, so you must add use-default- to specify the scanning annotation filters="false" configuration.

2.context:exclude-filter

In addition, in the project I participated in, it can be found that some subpackages in the package specified by base-package do not contain annotations, so there is no need to scan. At this time, you can specify context:exclude-filter to filter, indicating that this package does not need to be scanning

Case implementation:

<context:component-scan base-package="com.huawei.*" use-default-filters="false">  
	<context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller"/>   
</context:component-scan> 

3. Summary

<context:exclude-filter>指定的不扫描
<context:include-filter>指定的扫描

Guess you like

Origin blog.csdn.net/gjb760662328/article/details/129205543