SpringBoot学习日记2 配置标签学习

字节浮尘
• 阅读 1058

SpringBoot学习日记

配置文件

默认配置文件名:

  • application.properties
  • application.yml
1、YAML

properties:

server.port : 8080

yml

server:
    port:8080
基本语法:
  • K: V 表示键值对(冒号后面有空格)
  • 空格来控制层级关系,左对齐为同一层级
  • 大小写敏感
值的写法:
  • 字面量 普通的值(数字、字符串、布尔)k: V

    字符串默认不用加引号,而且单引号和双引号不一样。双引号里面的特殊符号会被转义。

  • 对象,map(属性和值)

    K-V写法

    Friend:
    lastName: zhansan
    age: 20

    行内写法:

    Friend: {lastName: zhansan,age: 18}
  • 数组

    写法1 - xxx

    Pets:
        - cat
        - dog

##### 实践:

###### YML配置

People.class

// 必须为容器中的组件
@Component
// 配置文件哪个下面的所有属性 进行映射
@ConfigurationProperties(prefix = "people")
public class People {

private String name;
private Boolean gender;
private Integer age;
private Map<String,String> books;
private List<String> lists;
private Dog dog;

}


Dog.class

@Component
public class Dog {

private String name;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@Override
public String toString() {
    return "Dog{" +
            "name='" + name + '\'' +
            '}';
}

}


application.yml

people:
name: 'afsun'
gender: true
age: 24
books:

k1: v1
k2: v2

dog:

name: 小黑

lists:

- a1
- a2
- a3
- a4
- b1

pom.xml

<dependency>

<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-configuration-processor</artifactId>
<optional> true </optional>

</dependency>


配置文件处理器:帮我们生成配置文件元数据,在配置文件件中可以生成提示    

测试

package com.example.demo;

import com.example.demo.bean.People;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**

  • 单元测试
  • 可以在测试期间,自动注入

*/
@RunWith(SpringRunner.class) // 指定使用Spring的单元测试而不是Junit
@SpringBootTest
public class DemoApplicationTests {

@Autowired
People p1 ;

@Test
public void contextLoads() {
    System.out.println(p1);
}

}


###### properties方式

配置People

people.age=17
people.gender=true
people.name=afsun
people.books.key1 = v1
people.books..key2 = v2
people.lists=a,b,c
people.dog.name = 花花


输出:

People{name='afsun', gender=true, age=17, books={key1=v1, key2=v2}, lists=[a, b, c], dog=Dog{name='����'}}


###### @Value的使用

<bean class = "People" >

<!--${key}从环境变量、配置文件中获取、#{spEL} EL表达式-->
<property name="xxx" value="字面量/${key}/#{spel}" ></property>

</bean >


使用:

@Component
//@ConfigurationProperties(prefix = "people")
public class People {

@Value("#{11*22}")
private String name;
@Value("${people.gender}")
private Boolean gender;
private Integer age;
private Map<String,String> books;
private List<String> lists;
private Dog dog;

}


输入:

People{name='242', gender=true, age=null, books=null, lists=null, dog=null}


#### 2、@Value和@ConfigurationProperties区别

|                     | @ConfigurationProperties |        @Value        |
| :-----------------: | :----------------------: | :------------------: |
|        功能         | 批量注入配置文件中的属性 | 手动的,一个个的指定 |
|      松散绑定       |           支持           |        不支持        |
|        SpEL         |          不支持          |         支持         |
|   JSR303数据校验    |           支持           |        不支持        |
| 复杂类型封装(map) |           支持           |        不支持        |

如果只是获取配置文件中的某个值那么久用@Value

如果专门写一个JavaBean的配置文件则用@ConfigurationProperties

JSR303数据校验

@Component
@Validated
@ConfigurationProperties(prefix = "people")
public class People {

@Email
private String name;
private Boolean gender;
private Integer age;
private Map<String,String> books;
private List<String> lists;
private Dog dog;

}

people.age=17
people.gender=true
people.name=afsun
people.books.key1 = v1
people.books..key2 = v2
people.lists=a,b,c
people.dog.name = gougou


输出

Description:

Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'people' to com.example.demo.bean.People failed:

Property: people.name
Value: afsun
Origin: class path resource [application.properties]:4:13
Reason: 不是一个合法的电子邮件地址

修改:

����People

people.age=17
people.gender=true
people.name= ashuash12123@qq.com
people.books.key1 = v1
people.books..key2 = v2
people.lists=a,b,c
people.dog.name = gougou


输出正常

People{name='ashuash12123@qq.com', gender=true, age=17, books={key1=v1, key2=v2}, lists=[a, b, c], dog=Dog{name='gougou'}}


松散绑定:people.lastName = people.last-name*

#### 3、@PropertySource

告诉SpringBoot加载哪个配置文件。

@PropertySource(value={"classpath:people.properties"})
@component
@configurationProperties(prefix = "")
public class people {
.....
}


@ImportRsource

导入Spring的配置文件,让配置文件生效

SpringBoot没有Spring的配置文件,我们写的配置文件不能自动是被,如果需要Spring配置文件生效则需要用此注解标注在类上(主配置类)。

@ImportResource(locations = {""})、
@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
}

}


SpringBoot推荐使用全注解的方式,使用配置来完成

// 注明为配置类
@configuration
public class MyConfig{

// 返回值添加到容器中,方法名为id名
@Bean
public BeanFactory getBeanFactory(){
    /*
    
    */
}

}


#### 4、@ 配置文件占位符

##### 1、随机数

${random.value}
${random.int}
${random.long}
${random.int(10)}
${random.int[1024.6454]}




##### 2、占位符

获取之前配置的值,如果没有值可以用:指定默认值

People

people.age=17
people.gender=true

随机数

people.name= ${radom.uuid}
people.books.key1 = v1
people.books..key2 = v2
people.lists=a,b,c

指示一个默认值

people.dog.name = ${people.name:afsun}_dog


#### 5、Profile

为了切换各种环境,生产环境、开发环境、测试环境

我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml

默认使用的是==application.properties==

##### 多文档

多文档:application-dev.properties、application-prod.properties....

application.properties

spring.profiles.active = dev


##### yml文档块

application.yml

server:

port:8083

spring:

profiles
    active:dev --指定版本

------下面是文档块
---开发环境
server:

port:8083

spring:

profiles:dev

---生产环境
server:

port:8084

spring:

profiles:prod

---测试环境
server:

port:8085

spring:

profiles:test

##### 命令行方式选定

program arguments:--spring.profiles.active=xxx

JVM -Dspring.profiles.active = xxx

#### 6、配置文件加载位置

- -file:./config/
- -file:./
- -classpath:/config/
- -classpath:/

优先级从高到低的顺序,高优先级覆盖低优先级内容,SpringBoot4个位置的文件全部读取,**互补配置**

==spring.config.location== 指定默认的配置文件路径,运维时使用。

#### 7、外部配置加载顺序

可以从以下位置加载配置,符合上面的**互补配置**

- 命令行参数 `--server.port=xxx` 多个配置用空格分开
- jar包外部的application-{profile}.properties或application.yml(带spring.profile)(jar包外目录)
- jar包内部的application-{profile}.properties或application.yml(带spring.profile)
- jar包外部的application.properties或application.yml
- jar包内部的application.properties或application.yml
- @Configuration注解类上的@PropertySource
- 通过SpringApplication.setDefaultProperties指定

#### 8、自动配置原理

1. SpringBoot启动的时候加载主配置类,开启配置功能==@EnableAutoConfiguration==

2. @EnableAutoConfiguration

   - 获取`META-INF/spring.factories` 中key为EnableAutoConfiguration.class的类名加入到容器中

   - ```properties
     # Auto Configuration Import Listeners
     org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
     org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener
     
     # Auto Configuration Import Filters
     org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
     org.springframework.boot.autoconfigure.condition.OnBeanCondition,\
     org.springframework.boot.autoconfigure.condition.OnClassCondition,\
     org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition
     
     # Auto Configure
     org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
     org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
     org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
     org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
     org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
     org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
     org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
  • 自动配置类进行自动配置功能
  •   @Configuration //为配置类
      @EnableConfigurationProperties({HttpProperties.class}) //启用onfigurationProperties功能,指定类的,从配置文件中获取指定的数据,并把HttpProperties加入到容器中
      @ConditionalOnWebApplication // Spring底层@Condition注解,判断当前应用是否是web应用
          type = Type.SERVLET
      )
      @ConditionalOnClass({CharacterEncodingFilter.class})// 判断当前项目是否有这个类
      @ConditionalOnProperty( // 判断配置文件中是否存在某个配置:prefix。。。
          prefix = "spring.http.encoding",
          value = {"enabled"},
          matchIfMissing = true // 不存在,也是正确的
      )
      public class HttpEncodingAutoConfiguration {
          
          /**
          *通过有参构造器获得HttpProperties而HttpProperties映射了配置文件
          */
         public HttpEncodingAutoConfiguration(HttpProperties properties) {
              this.properties = properties.getEncoding();
          }
          @Bean
          @ConditionalOnMissingBean //当beanFactory没有该bean时
          public CharacterEncodingFilter characterEncodingFilter() {
              CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
              filter.setEncoding(this.properties.getCharset().name());
                      filter.setForceRequestEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.REQUEST));
              filter.setForceResponseEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.RESPONSE));
              return filter;
          }
      }
  • HttpProperties.class

    所有在配置文件中能配置的属性都是在xxxProperties类中封装,配置文件能配置什么就可以参照某个功能对应的这个属性类。

    @ConfigurationProperties(
        prefix = "spring.http"
    ) //根据"spring.http"可以在配置文件中,指定属性
    public class HttpProperties {

    根据当前的不同条件判断,决定这个配置类是否生效

xxxxAutoConfiguration:自动配置类

给容器添加相应的组件

xxxproperties类作为配置文件的映射类

自动配置实现

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Easter79 Easter79
3年前
spring注解
随着越来越多地使用Springboot敏捷开发,更多地使用注解配置Spring,而不是Spring的applicationContext.xml文件。Configuration注解:Spring解析为配置类,相当于spring配置文件Bean注解:容器注册Bean组件,默认id为方法名@Configurat
Stella981 Stella981
3年前
Egret入门学习日记
第四篇(书中3.1~3.3内容)  好了,今天继续把昨天的问题解决了。  今天见鬼了。  !(https://oscimg.oschina.net/oscnet/e6f4fcdbb3e84689e44dc5a99acbf338e78.png)  现在界面又出来了。唯一我动过的地方,应该就是这里:    !(https://os
Stella981 Stella981
3年前
Elasticsearch学习(3) spring boot整合Elasticsearch的原生方式
前面我们已经介绍了springboot整合Elasticsearch的jpa方式,这种方式虽然简便,但是依旧无法解决我们较为复杂的业务,所以原生的实现方式学习能够解决这些问题,而原生的学习方式也是Elasticsearch聚合操作的一个基础。一、修改springboot的application.properties配置文件端口号
Stella981 Stella981
3年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Stella981 Stella981
3年前
SpringBoot2 学习 集成Druid配置
Mavenspring.datasource.druid.webstatfilter.principalsessionnamesession_name测试http://localhost:9081/mixmall/druid/index.html————————————————版权
Stella981 Stella981
3年前
RabbitMQ 学习日记
RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.cnblogs.com%2Fzlfoak%2Fp%2F5521673.html)
Easter79 Easter79
3年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Stella981 Stella981
3年前
Spring Boot配置文件详解
一、主配置文件SpringBoot默认主配置文件名为application.yml或者application.properties1.yml和properties1.1yml语法:key:空格value同一个层级的缩进tab或者空格必须相同
Easter79 Easter79
3年前
SpringBoot2 学习 集成Druid配置
Mavenspring.datasource.druid.webstatfilter.principalsessionnamesession_name测试http://localhost:9081/mixmall/druid/index.html————————————————版权