Understanding the @SpringBootApplication annotation - how to exclude automatic assembly & how to automatically load in distributed situations & how nacos was discovered

Preface

As the mainstream open source framework for Java Web development, spring is the most successful framework in the Java world. Continuous and in-depth understanding of the spring framework is the constant pursuit of Java programmers.

This blog introduces the automatic loading related content of SpringBootApplicant annotations

A list of other relevant Spring blog posts is as follows:

Spring basics (core container) - from configuration files to annotation development & object creation + member variable assignment IOC & enhanced method AOP

Insert image description here
Spring Advanced (AOP Understanding) - Static/Dynamic Proxy & Aspect Oriented Programming AOP (Aspect Oriented Programming) & Logging & Enhancement Methods

Insert image description here

Spring Advanced (AOP Application) - The problem of invalid access to the private method of the controller layer after dynamically proxying AOP

Insert image description here

Spring Basics (Web-MVC) - Create a new springWeb project in idea & browser request and server response & SpringMvc file related

Insert image description here
SpringMvc Framework - [In-depth] SpringMVC operation process: full process analysis from client sending request to springMvc framework returning response & DispatcherServlet

Insert image description here

SpringBoot basics - tracing back to the source what is servlet, what is tomcat, what is maven & preliminary springboot project, maven build, packaging & testing

Insert image description here

lead out


1. No configuration is written when importing mybatis-related packages, and the @SpringBootApplication annotation is added to exclude the relevant content of automatic assembly;
2. Under the distributed architecture, if a module wants classes under other modules, it needs to add @ComponentScan(basePackages = “com .tianju.domain") to scan the package;
3. If you do not add an annotation for package scanning, you need to create a new folder META-INF under the resource of the module you want to be injected into the spring container, that is, in the root directory. File spring.factories, write relevant content;

Mybatis related package import does not write configuration

1.@SpringBootApplication annotation

Insert image description here

2. If you do not write URL and other related configurations

The dependencies of mysql, mybatis, and druid are imported, but there is no configuration to write the relevant data source URLs in the application.yml file.

Insert image description here

At this time, you need to add relevant content to exclude automatic assembly in the annotation @SpringBootApplication; after adding the exclusion, start the project again and no more errors will be reported.

Insert image description here

package com.tianju;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude =
        {
    
    DruidDataSourceAutoConfigure.class,
        DataSourceAutoConfiguration.class}
)
public class TestApp {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(TestApp.class);
    }
}

How to load classes of other modules under distributed architecture

Insert image description here

Mybatisplus uses paging plug-in and interceptor strategy. When writing the interceptor at an import location, and other modules import configuration classes, you need to use @ComponentScan(base="the package where the configuration class is located)

1. General pattern of monolithic architecture

Insert image description here

package com.tinaju.bm.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 分页需要加一个拦截器
 */
@Configuration
public class MybatisPlusConfig {
    
    

    @Bean // 放到容器中
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
    
    
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        paginationInnerInterceptor.setOverflow(true);
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
        return mybatisPlusInterceptor;
    }
}

In single mode, the annotations below the main startup class will be automatically scanned, and the paging interceptor of @Configuration marked with the annotation will be scanned.

Insert image description here

2. Distributed architecture model

In a distributed architecture, if a module wants classes from other modules, it needs to add @ComponentScan(basePackages = “com.tianju.domain”) to scan the packages.

Insert image description here

package com.tianju.smovie.genre;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableDiscoveryClient
//@EnableJpaRepositories // 不加会报错 Not a managed type: class com.tianju.domain.entity.Genre
@EntityScan(basePackages = "com.tianju.domain.entity") // TODO:jpa需要加这个
@ComponentScan(basePackages = "com.tianju.domain")
public class GenreApp {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(GenreApp.class);
    }
}

If not added, it will not be loaded into the container.

Insert image description here

Another way: Set up autowiring

If you do not add the bread scanning annotation, you need to create a new folder META-INF under the resource of the module you want to inject into the spring container, that is, in the root directory, create a new file spring.factories, and write relevant content.

Insert image description here

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.tianju.domain.config.MybatisPlusConfig

Insert image description here

Exclusion related

Insert image description here

Question: How did you find out when using nacos?

As shown in the figure below, there is a folder META-INF in nacos, and there is a file spring.factories below, and the contents inside will be loaded.

Insert image description here

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration,\
  com.alibaba.cloud.nacos.endpoint.NacosDiscoveryEndpointAutoConfiguration,\
  com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration,\
  com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration,\
  com.alibaba.cloud.nacos.discovery.reactive.NacosReactiveDiscoveryClientConfiguration,\
  com.alibaba.cloud.nacos.discovery.configclient.NacosConfigServerAutoConfiguration,\
  com.alibaba.cloud.nacos.loadbalancer.LoadBalancerNacosAutoConfiguration,\
  com.alibaba.cloud.nacos.NacosServiceAutoConfiguration,\
  com.alibaba.cloud.nacos.util.UtilIPv6AutoConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
  com.alibaba.cloud.nacos.discovery.configclient.NacosDiscoveryClientConfigServiceBootstrapConfiguration
org.springframework.boot.SpringApplicationRunListener=\
  com.alibaba.cloud.nacos.logging.NacosLoggingAppRunListener

Summarize

1. No configuration is written when importing mybatis-related packages, and the @SpringBootApplication annotation is added to exclude the relevant content of automatic assembly;
2. Under the distributed architecture, if a module wants classes under other modules, it needs to add @ComponentScan(basePackages = “com .tianju.domain") to scan the package;
3. If you do not add an annotation for package scanning, you need to create a new folder META-INF under the resource of the module you want to be injected into the spring container, that is, in the root directory. File spring.factories, write relevant content;

Guess you like

Origin blog.csdn.net/Pireley/article/details/133383051