springboot配置文件

1、配置文件
springboot使用一个全局的配置文件,配置文件是固定的
application.properties
application.yml

配置文件的作用:修改SpringBoot自动配置的默认值;Springboot在底层都给我们配置好了

YAML(YAML AlN’T MArkup Language)
Yaml a markup language:是一个标记语言
Yaml isn’t markup language:不是一个标记语言
标记语言:
以前都配置文件:大多xml文件
yaml:以数据为中心,比json、xml等更合适做配置文件;
YAML:配置例子
server:
port:8080

xml:
8080

2、YAML语法
1、基本语法
k:(空格)v :表示一对键值对(空格必须有)
以空格的缩进来控制层级关系,只要是左对齐的一列数据,都是同一个层级的
server:
port:8080
path:/hello
属性和值也是大小写敏感

2、值的写法
字面量:普通的值(数字、字符串、布尔)
**k: v :字面直接类写**
	字符串默认不用加上单引号或双引号;
	“”:双引号,不会转译字符里面的特殊字符,特殊字符会作为本身想表达的意思
		name:“zouxuyin \n list”   输出:zouxuyin 换行  list
	'':  单引号;会转译为特殊字符,特殊字符只是一个普通的字符串数据
		name:'zouxuyin ln list'  输出:zouxuyin \n list
		
**对象、map(属性和值)(键值对)**
k: v : 在下一行类写对象的属性和值的关系;注意缩进
		对象还是k: v的方式
			friends:
				lastName:zouxuyin
				age:20
		行内写法
		friends:{lastName:zouxuyin,age:20}

数组(List,set):
用- 值表示数组中的一个元素
pets:
 - cat
 - dog
 - pig
 行内写法
 pets:[cat,dog,pig]

配置文件值注入
yml配置文件

person:
lastname: zouxuyin
age: 18
boss: false
birth: 2020/11/1
maps: {k1: xuyin,k2: 12}
lists:
- yin
- zouzou

dog:
name: 小狗
age: 12

javaBean:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**

  • 将配置文件中配置的每一个属性的值,映射到这个数组中
  • @ConfigurationProperties:告诉SpringBoot将文本类中的所有属性和配置文件中相关的配置进行绑定;
  • prefix = “person”:配置文件中下面的所有属性进行一一映射
  • 只有这个组件是容器的组件用Component,才能容器提供的@ConfigurationProperties功能
    */

@Component
@ConfigurationProperties(prefix = “person”)
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;

private Map<String,Object> maps;
private List<Object> lists;

private  Dog dog;

其中需要导入ConfigurationProperties依赖

org.springframework.boot spring-boot-configuration-processor true

在properties配置文件中
#idea,properties使用utf-8
#配置person的值
person.last-name=z邹邹
person.age=20
person.birth=2020/3/2
person.boss=false
person.dog.name=dog1
person.maps.k1=zzz
person.maps.k2=xxx
person.lists=a,b,c,d

2、@Value获取值和@ConfigurationProperties获取值比较
| |@ConfigurationProperties |@Value
功能 |-批量注入配置文件中的属性- |-一个个指定-|
松散绑定(松散语法)| 支持 |不支持 ------------lastName和last-name的比较
SpEL 不支持 支持
JSR303数据校验 支持 不支持
复杂类型封装 支持 不支持

配置文件yml还是properties他们都能获取到值
如果说,我们只是在某个业务逻辑中获取一下配置文件中的某项值,使用@Value;
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们之间使用@ConfigurationProperties;
3、配置文件注入值数据校验
@Component
@ConfigurationProperties(prefix = “person”)
@Validated
public class Person {
//lastName必须是邮箱格式
@Email
private String lastName;

4、@PropertySource&@ImportResource
@PropertySource:加载指定的配置文件
@PropertySource(value = {“classpath:person.properties”})
@Component
@ConfigurationProperties(prefix = “person”)
//@Validated
public class Person {

/**
 * <bean class="Person">
 *     <property name="lastname" value="字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property>
 * </bean>
 *
 */

// @Value("${person.last-name}"
//lastName必须是邮箱格式
//@Email
private String lastName;
@Value("#{11*2}")
private Integer age;
@Value(“true”)
private Boolean boss;

@ImportResource:导入spring的配置文件,让配置文件里面的内容生效;
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,加载进来**@ImportResource**标注在一个配置(主)类上
@ImportResource(locations = {“classpath:beans.xml”})
导入Spring的配置文件让其生效

现在一般不来编写Spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>



SpringBoot推荐给容器中添加组件的方式,推荐使用全注解的模式

1、配置类===Spring配置文件
2、使用@Bean给容器中添加组件
/**

  • @Configuration:指明当前类是一个配置类,就是来替代之前的Spring配置文件
  • 在配置文件中用标签添加组件
    */
    @Configuration
    public class MyAppConfig {
    //将方法的返回值添加到容器中,容器中这个组件默认的id就是方法名
    @Bean
    public HelloService HellloService02(){
    System.out.println(“配置类@Bean给容器中添加组件了。。。。”);
    return new HelloService();
    }
    }

四、配置文件占位符application.properties文件中
#person.las-name=张三 r a n d o m . u u i d 占 位 符 p e r s o n . l a s t − n a m e = z 邹 邹 {random.uuid}占位符 person.last-name=z邹邹 random.uuidperson.lastname=z{random.uuid}200
person.age= r a n d o m . i n t p e r s o n . b i r t h = 2020 / 3 / 2 p e r s o n . b o s s = f a l s e p e r s o n . d o g . n a m e = {random.int} person.birth=2020/3/2 person.boss=false person.dog.name= random.intperson.birth=2020/3/2person.boss=falseperson.dog.name={person.last-name}dog1_
person.dog.age=3
person.maps.k1=zzz
person.maps.k2=xxx
person.lists=a,b,c,d

五、Profile
1、多Profile文件
我们在主配置文件编写的时候,文件可以是application-{profile}.properties/yml
默认使用application.properties的配置

2、yml支持多文档块方式
server:
port:8080
srping:
spring:
profiles:
active: prod

server:
port: 8081
spring:
profiles:dev

server:
port: 8082
spring:
profiles:prod

3、激活指定profile
1、在配置文件中指定spring.profiles.active=dev
2、命令行:
—spring.profiles.active=dev
在这里插入图片描述打开包的文件位置,cmd
java -jar spring-boot-01-helloworld-quick-0.0.1-SNAPSHOT.jar --spring.profile.active=prod启动文件
可以在测试的时候,
3、虚拟机参数
-Dspring.profiles.action=dev

六、配置文件加载位置
springboot启动会扫描以下位置的application.properties/yml
-file: …/config
-file: …/
-classpath:/config
-classpat:/
以上按照优先级从到低,优先级高的配置会覆盖低优先级,也可以通过spring.config.location来改变默认配置
Springboot会从四个位置全部加载主配置文件,互补配置

七、外部配置加载顺序
springboot支持多种外部配置方式,以下加载配置从高到低:高优先级的配置覆盖低优先级配置,所有的配置会形成互补配置
1、命令行参数
jar -jar spring-boot-02-config-02-0.01-SNAPSHOT.jar --server.port=8081
多个配置用空格分开:–配置项=值

2、来自java:comp/env的NDI属性
3、java系统属性(system.gtProperties())
4、操作系统环境变量
5、RandomValuePropertySource配置的random.*的属性
由jar包外向jar内加载
优先加载带profile
6、jar包外包部的application-(profile).properties或者application.yml(带spring.profile)配置文件
7、jar包内部的application-(profile).properties或者application.yml(带spring.profile)配置文件
8、jar包外部的application-(profile).properties或者application.yml(不带spring.profile)配置文件
9、jar包内部的application-(profile).properties或者application.yml(不带spring.profile)配置文件

10、@Configuration注解类上的@PropertySource
11、通过SpringApplicationsetDefaultProperties指定的默认属性

八、自动配置原理

配置文件到底能写什么?怎么写?自动配置原理
配置文件能配置的属性参照:参照文档

自动配置原理:
1、springboot启动的时候加载主配置类,开启了自动配置功能@EnableAutoConfiguration
2、@EnableAutoConfiguration作用:
利用AutoConfigurationImportSelector给容器中导入一些组件
List configurations = this.getCandidateConfigurations(annotationMetadata, attributes);获取候选的配置
在这里插入图片描述在这里插入图片描述在这里插入图片描述
精髓:
1、Springboot启动会加载大量的自动配置类
2、我们看我们需要的功能有没有SpringBoot默认写好的自动配置类;
3、我们再来看这个自动配置类中到底配置了那些组件,(只要我们用的组件有,我们就不需要来配置了),没有就需要自己来写
4、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性,我们 就可以在配置文件中指定这些属性的值;
xxxAutoConfiguration:自动配置类;
给容器中添加组件
xxxProperties:封装配置文件中相关的属性

细节:
1、@Condition派生注解(Spring注解版原生的@Condition作用
作用:必须是@Conditioin指定的条件成立,才给容器中添加组件,配置里面的所有内容才能生效;
在这里插入图片描述自动配置类必须在一定的条件才能生效
那些配置类生效,
在配置文件中application.properties中
debug=true;属性类让控制台打印自动配置报告,这样就可以方便都知道那些自动配置类生效;
Positive matches:
-----------------自动配置类启用了

AopAutoConfiguration matched:
- @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)

AopAutoConfiguration.ClassProxyingConfiguration matched:
- @ConditionalOnMissingClass did not find unwanted class ‘org.aspectj.weaver.Advice’ (OnClassCondition)
- @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)

DispatcherServletAutoConfiguration matched:
- @ConditionalOnClass found required class ‘org.springframework.web.servlet.DispatcherServlet’ (OnClassCondition)
- found ‘session’ scope (OnWebApplicationCondition)

DispatcherServletAutoConfiguration.DispatcherServletConfiguration matched:
- @ConditionalOnClass found required class ‘javax.servlet.ServletRegistration’ (OnClassCondition)
- Default DispatcherServlet did not find dispatcher servlet beans (DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition)

Negative matches:
-----------------没有启用,没有匹配成功
ActiveMQAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class ‘javax.jms.ConnectionFactory’ (OnClassCondition)

AopAutoConfiguration.AspectJAutoProxyingConfiguration:
Did not match:
- @ConditionalOnClass did not find required class ‘org.aspectj.weaver.Advice’ (OnClassCondition)

ArtemisAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class ‘javax.jms.ConnectionFactory’ (OnClassCondition)

BatchAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class ‘org.springframework.batch.core.launch.JobLauncher’ (OnClassCondition)

CacheAutoConfiguration:
Did not match:
- @ConditionalOnBean (types: org.springframework.cache.interceptor.CacheAspectSupport; SearchStrategy: all) did not find any beans of type org.springframework.cache.interceptor.CacheAspectSupport (OnBeanCondition)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值