springboot启动类:
//spring快速启动
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class);
}
}
启动类上有一个注解@SpringBootApplication,这个注解中主要的核心注解有以下几个:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
//1.配置注解
@SpringBootConfiguration
//2.自动配置注解
@EnableAutoConfiguration
//3.注解扫描
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
1.配置注解
@SpringBootApplication内部有@SpringBootConfiguration注解,而@SpringBootConfiguration注解中又依赖于@Configuration注解,configuration就是springbean的配置注解。因此在有SpringBootApplication或者SpringBootConfiguration注解的类中,可以使用注解的方式配置bean:
@Bean
public Object getObj(){
return null;
}
2.自动配置注解EnableAutoConfiguration
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
//导入了一个自动配置导入选择器
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
点入AutoConfigurationImportSelector中间有一段代码:
//得到所有的类名
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
//这里看出,这个方法会去找META-INF/spring.factories下的文件
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
return configurations;
}
这里所能看见这些类名,后缀基本都是AutoConfiguration,自动配置类,这里可以随意找一个自动配置类看一看:
package org.springframework.boot.autoconfigure.data.redis;
import java.net.UnknownHostException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
@Configuration
@ConditionalOnClass({RedisOperations.class})
//这个注解加载了一个配置
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
点进去继续看RedisOperations.class
//加载配置文件
@ConfigurationProperties(
//配置文件前缀
prefix = "spring.redis"
)
public class RedisProperties {
所加载的配置在配置文件中搜索一下
{
"sourceType": "org.springframework.boot.autoconfigure.data.redis.RedisProperties",
"defaultValue": 6379,
"name": "spring.redis.port",
"description": "Redis server port.",
"type": "java.lang.Integer"
}
可以看到自动设置的一些配置。
这就是springboot自动配置加载的大致流程。如果这个流程大致了解之后,我们就可以修改springboot的一些自带的配置
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
在springboot要加载的配置文件中加上要修改的键值对即可。
3.注解扫描ComponentScan
将会扫描SpringBootApplication启动类同级包下(包括子包)的所有注解。
以上大致就是springboot启动的三个比较核心的注解。