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:
Table of contents
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
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.
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.
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
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
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.
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.
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.
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.
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tianju.domain.config.MybatisPlusConfig
Exclusion related
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.
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;