02-springboot配置

目录

 

1,前言

2,YAML介绍

3,获取yml配置文件内容

4,springboot的配置文件

5,springboot使用@Value实现映射

6,@PropertySource、@ImportResource和@Bean注解

7,Springboot的占位符

8,profile

9,配置文件存放目录优先级(从高到低)

10,总结


1,前言

主要讲的有yaml语法,配置文件,配置文件加载顺序,配置文件配置原理。

2,YAML介绍

     A:什么是YAML

           YAML(/ˈjæməl/,尾音类似camel骆驼)是一个可读性高,用来表达数据序列化的格式。内容是一个键值对,所以它是与数据为中心,更加适合作为配置文件。文件后缀是以yml结尾,用于springboot的配置文件中,默认的配置文件名为:application.yml。

   B:YAML语法

         a:基本语法

            K:(空格)V:表示一个键值对,在冒号后面必须有一个空格,空格表示的是层级关系,如果多行的时候是对齐的,表示是同级关系:

server:
 port: 8080

注意:属性和值之间是区分大小写的;双引号不会转义字符(name:  "zhangsan \n lisi":输出;zhangsan 换行 lisi );

单引号会转义字符(name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi );字符串可以不需要引号。

       b:值的写法

           字面值:

                k: v :字面直接来写;

           对象。Map:

              k: v:在下一行来写对象的属性和值的关系;注意缩进 ,对象还是k: v的方式;

friends:
  lastName: zhangsan 
  age: 20

行内写法:

friends: {lastName: zhangsan,age: 18}

          

        集合:

用- 值表示数组中的一个元素

     

pets: 
  ‐ cat 
  ‐ dog
  ‐ pig

       行内写法:

pets: [cat,dog,pig] 

3,获取yml配置文件内容

     A:编写两个实体类Person,Student,用于存放属性。

Person类:

@Component
@ConfigurationProperties(prefix="Person")
public class Person {
     private int age;
     private String name;
     private Map<String,String> map;
     private List<String> list;
     private Student student;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	@Override
	public String toString() {
		return "Person [age="+ age + ", name=" + name + ", map=" + map + ", list=" + list + ", student=" + student
				+ "]";
	}
     
}

Student类:


public class Student {
	private int age;
    private String name;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Student [age=" + age + ", name=" + name + "]";
	}
    
}

      编写yml配置文件:

server:
	port: 8080

Person:
	age: 18
	name: xiaozhi
	map:
		k1: we
		k2: wo
	list:
		-haha
		-hoho
	student:
		age: 20
		name:xiaofang

这是需要将我们通知springboot该类类中的所有属性和配置文件中相关的配置进行绑定,使用的注解为:@ConfigurationProperties:该注解有个属性prefix ,值为类,配置文件中哪个类下面的所有属性进行映射。

同时还需要将给类加入到springboot组件,需要的注解为:@Component

还需要在pom文件添加一个坐标,该坐标表示的是扫描配置文件,文件校验,可以不给出,如果不给,编写yml配置文件不给出提示和校验。

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

B:在主程序运行结果(可使用单元测试)

控制类:

@Controller
public class AppController {
	@Resource
	private Person person;
	@ResponseBody
	@RequestMapping("/show")
   public String show(){
		System.out.print(person);
	   return "你好!世界";
   }
}

测试结果为:

可以看到Person类的属性以及被注入了。

4,springboot的配置文件

     A:springboot给出两种配置文件

           •application.properties

           •application.yml

配置文件默认为固定文件名,springboot可以将自动将文件扫描到容器中。之前一起讲了yml配置文件的编写方法和加载方式。项目将会说properties 文件的编写和加载方式;

    B:编写properties 文件

Person.age=18
Person.name=xiaozhi
Person.map.k1=we
Person.map.k2=wuw
Person.list=we.weo.werf
Person.student.age=20
Person.student.name=xiaofang

只要编写程这样就可以了,加载方式和yml的一样,结果如下图所示:

5,springboot使用@Value实现映射

 之前使用@ConfigurationProperties(prefix="Person")这个注解实现配置文件到类属性的映射,@ConfigurationProperties注解表示的是将该类的全部属性实现映射,只要配置文件有对应的属性,就将配置文件的内容注入到类中。而@Value是单个注入,分别对类的属性进行注入。

   A:使用@Value实现注入

 只需要通过修改Person类就可以了,如下段代码:

@Component
//@ConfigurationProperties(prefix="Person")
public class Person {
	@Value("${Person.age}")
     private int age;
     private String name;
     private Map<String,String> map;
     private List<String> list;
     private Student student;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	@Override
	public String toString() {
		return "Person [age="+ age + ", name=" + name + ", map=" + map + ", list=" + list + ", student=" + student
				+ "]";
	}
     
}

测试结果:

 

可以发现,只是在age属性上加上@Value时,注入值只是注入这个属性的值。

  B:@Value和@ConfigurationProperties的比较

6,@PropertySource、@ImportResource和@Bean注解

     A:PropertySource:读取指定的文件(只适用于Properties文件)

          属性:value:String[]

写一个a.Properties文件

Person.age=20
Person.name=xiao
Person.map.k1=hahah
Person.map.k2=xixi
Person.list=we,weo,werfwowowoowowo
Person.student.age=210
Person.student.name=xiaofan

           在Person类上添加注解

@PropertySource(value={"classpath:a.properties"})
@Component
@ConfigurationProperties(prefix="Person")
public class Person {
	
     private int age;
     private String name;
     private Map<String,String> map;
     private List<String> list;
     private Student student;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	@Override
	public String toString() {
		return "Person [age="+ age + ", name=" + name + ", map=" + map + ", list=" + list + ", student=" + student
				+ "]";
	}
     
}

运行结果:

值得注意的是使用此注解扫描时,如果配置文件目录有一个application.properties文件,而且该文件中存在该类的配置,它会优先使用默认配置文件而不去加载自定义配置文件。

B:@ImportResource注解:用于加载spring配置文件

       在学习spring的时候,spring的配置文件主要配置的是bean对象,springboot页提供了一个扫描器用于加载spring的配置文件。该注解就是扫描spring配置文件所使用的。

编写一个spring的配置文件

<?xml version="1.0" encoding="UTF‐8"?> <beans 
xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema‐instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring‐beans.xsd"> 
<bean id="helloService" class="com.atguigu.springboot.service.HelloService"></bean> 
</beans>

在主程序类中加入注解:

@ImportResource(locations = {"classpath:beans.xml"})

就可以将该bean对象注入到sprig容器中。

C:@Bean标签

spring不建议我们使用@ImportResource注解,主要是会导致开发的周期过长,编写spring配置文件过于麻烦。为了解决这个问题,springboot提供@Bean注解,将类注入到spring容器中。使用该注解需要指定需要配置的类,@Configuration注解就可以指定配置类。

@Configuration//指明当前类是一个配置类;就是来替代之前的Spring配置文件
public class Person {
     private int age;
     private String name;
     private Map<String,String> map;
     private List<String> list;
     private Student student;
     @Bean//在配置文件中用<bean><bean/>标签添加组件
     public void show(){}

7,Springboot的占位符

springboot在配置文件中可以定义占位符,通过占位符设置默认值,获取该配置文件其他值,拼接一些随机数等。

A:随机数

${random.value}、${random.int}、${random.long} 
${random.int(10)}、${random.int[1024,65536]}

springboot自己定义了一些获取随机数或者uuid的方法,如下所示:

Person.age=$(random.int)
Person.name=xiao$(random.uuid)
Person.map.k1=hahah
Person.map.k2=xixi
Person.list=we,weo,werfwowowoowowo
Person.student.age=210
Person.student.name=xiaofan

B:获取配置文件其他值:

Person.age=20
Person.name=xiao
Person.map.k1=hahah
Person.map.k2=xixi
Person.list=we,weo,werfwowowoowowo
Person.student.age=210${Person.age}
Person.student.name=xiaofan

表示获取到Person.age的值拼接到Person.student.age的值的后面,如果配置文件中没有属性和值,而配置类中有该属性,通过配置配置文件中的属性,将获取不到该值:

#Person.age=20
Person.name=xiao
Person.map.k1=hahah
Person.map.k2=xixi
Person.list=we,weo,werfwowowoowowo
Person.student.age=210${Person.age}
Person.student.name=xiaofan

如上面代码所示,将Person.age注释掉,将会导致出错。

C:设置默认值

Person.age=20
Person.name=xiao
Person.map.k1=hahah
Person.map.k2=xixi
Person.list=we,weo,werfwowowoowowo
Person.student.age=210${Person.hahah}
Person.student.name=xiaofan

这种上面代码的情况是如果配置文件中的占位符在配置类没有属性,没有给出默认值,表示的是字符拼接。

Person.age=20
Person.name=xiao
Person.map.k1=hahah
Person.map.k2=xixi
Person.list=we,weo,werfwowowoowowo
Person.student.age=210${Person.hahah:html}
Person.student.name=xiaofan

这种情况是给该属性赋值一个默认值,获取的时候是直接获取html这个值而不是直接拼接。

8,profile

springboot为我们提供了在不同环境使用不同配置文件的方式,例如配置端口号,在生产环境下使用8080端口,在测试环境下使用8081端口,开发环境使用8082端口,通过使用profile,可以让我们快速更换端口号

A:多个Profifile文件

文件命名格式:

application-{profifile}.properties/yml
例如:
application-dev.properties
application-dev.yml

B:properties配置文件使用profile功能

编写多个properties文件

application-prod.properties
server.port=8080

application-dev.properties

server.port=8081

激活profile:在默认配置文件application.properties中配置:

spring.profiles.active=dev

表示的是使用dev环境,也就是端口号为8081的配置。

C:yml配置文件使用profile

   这种方式比较简单,只需要编写在一个yml文件就可以了,这种方式在yml被叫做多文档块,通过"---"将一个yml文件分为多个文档块(三个横线),在最上面通过最上面的文档块进行激活:

spring: 
  profiles: 
    active: prod

全部的配置文件如下所示:

server:
 port: 8081 
spring: 
  profiles: 
    active: prod #激活prod环境
‐‐‐ 
server: 
  port: 8083 
spring: 
  profiles: dev 
‐‐‐ 
server: 
  port: 8084 
spring: 
profiles: prod #指定属于哪个环境

9,配置文件存放目录优先级(从高到低)

项目/config

项目目录项

classpath:config

classpath目录

10,总结

没有总结的总结

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值