一、YAML相关配置文件使用
1.配置文件类型
server.port=8080
server :
port : 8080
配置文件加载顺序:application.properties->application.yml->application.yaml
2.基本语法
大小写敏感 数据值前边必须有空格,作为分隔符 使用空格缩进表示层级关系,相同缩进为同一级 缩进不允许使用Tab键,只允许空格(idea会自动将table转换为空格)
3.数据格式
user :
age : 17
user : { age : 17 }
address :
- beijing
- shanghai
-
address : [ beijing, shanghai]
msg1 : 'hello \n world'
msg2 : "hello \n world"
user :
name : ${ userName}
二、Profile使用
1) profile用来完成不同环境下,配置动态切换的功能
2) profile配置方式
多profile文件方式:提供多个配置文件,每个代表一种环境
application-dev.properties/yml 开发环境 application-test.properties/yml 测试环境 application-prod.properties/yml 生产环境 yml多文档方式
3) profile激活方式
配置文件:在配置文件中配置 Spring.profiles.active=dev 虚拟机参数:在VM options指定:-Dspring.profiles.active=dev 命令行参数:java -jar xxx.jar --spring.profiles.active=dev
3) SpringBoot内部配置加载顺序
SpringBoot程序启动时,会从以下位置加载配置文件:
file:./config/ :当前项目下的/config目录下的配置 file./ :当前项目根目录下 classpath:/config/ :classpath的/config目录下 classpath:/ :classpath的根目录下 加载顺序为上下文排序顺序,所有配置互补生效,有冲突高优先级会生效
三、SpringBoot自动配置
1) condition条件接口模仿解析
通过自定义类实现condition接口的matches()方法得到想要的条件
public class ClassConditional implements Condition {
@Override
public boolean matches ( ConditionContext context, AnnotatedTypeMetadata metadata) {
boolean flag = true ;
Map < String , Object > annotationAttributes = metadata. getAnnotationAttributes ( ConditionOnClass . class . getName ( ) ) ;
String [ ] value = ( String [ ] ) annotationAttributes. get ( "value" ) ;
for ( String s : value) {
try {
Class . forName ( s) ;
} catch ( ClassNotFoundException e) {
flag = false ;
}
}
return flag;
}
}
@Target ( { ElementType . TYPE, ElementType . METHOD} )
@Retention ( RetentionPolicy . RUNTIME)
@Documented
@Conditional ( ClassConditional . class )
public @interface ConditionOnClass {
String [ ] value ( ) ;
}
@Configuration
public class UserConfig {
@Bean
@ConditionOnClass ( "redis.clients.jedis.Jedis" )
public User user ( ) {
return new User ( ) ;
}
@Bean
@ConditionalOnProperty ( name = "user" , havingValue = "zhangsan" , matchIfMissing = true )
public User user2 ( ) {
return new User ( ) ;
}
}
2) 依赖引用自动配置原理
@SpringBootApplication注解中添加有组合注解@EnableAutoConfiguration
@Target ( { ElementType . TYPE} )
@Retention ( RetentionPolicy . RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan (
excludeFilters = { @Filter (
type = FilterType . CUSTOM,
classes = { TypeExcludeFilter . class }
) , @Filter (
type = FilterType . CUSTOM,
classes = { AutoConfigurationExcludeFilter . class }
) }
)
public @interface SpringBootApplication {
@EnableAutoConfiguration中使用@Import({AutoConfigurationImportSelector.class})导入AutoConfigurationImportSelector类
@Target ( { ElementType . TYPE} )
@Retention ( RetentionPolicy . RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import ( { AutoConfigurationImportSelector . class } )
public @interface EnableAutoConfiguration {
AutoConfigurationImportSelector会从META-INF/spring.factories中加载所有的需要自动配置的类
protected List < String > getCandidateConfigurations ( AnnotationMetadata metadata, AnnotationAttributes attributes) {
List < String > configurations = new ArrayList ( SpringFactoriesLoader . loadFactoryNames ( this . getSpringFactoriesLoaderFactoryClass ( ) , this . getBeanClassLoader ( ) ) ) ;
ImportCandidates . load ( AutoConfiguration . class , this . getBeanClassLoader ( ) ) . forEach ( configurations:: add ) ;
Assert . notEmpty ( configurations, "No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you are using a custom packaging, make sure that file is correct." ) ;
return configurations;
}
3) 依赖引用自动配置模仿
添加配置类,通过读取配置文件加载(不需要加载可以不用写)
@Configuration
@EnableConfigurationProperties ( value = HelloProperties . class )
public class HelloServiceAutoConfiguration {
private HelloProperties helloProperties;
public HelloServiceAutoConfiguration ( HelloProperties helloProperties) {
this . helloProperties = helloProperties;
}
@Bean
@ConditionalOnMissingBean
public HelloService helloService ( ) {
return new HelloService ( helloProperties. getName ( ) , helloProperties. getAddress ( ) ) ;
}
}
在resources目录下添加META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.testStarter.config.HelloServiceAutoConfiguration
四、SpringBoot监控
1) SpringBoot监控概述
SpringBoot自带监控功能Actuator,可以帮助实现对程序内部运行情况监控,比如监控状态、Bean加载情况、日志信息等。
2) Actuator监控使用
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-actuator</ artifactId>
</ dependency>
访问http://localhost:8080/actuator