网站开发语言占有率,河南网站建设制作,营销网站定制公司,网页设计模板之家配置的概念#xff1a;
Spring Boot是基于约定的#xff0c;所以很多配置都有默认值#xff0c;但如果想使用自己的配置替换默认配置的话#xff0c;就可以使用application.properties或者application.yml(application.yaml)进行配置。
注意配置文件的命名必须是applicat…配置的概念
Spring Boot是基于约定的所以很多配置都有默认值但如果想使用自己的配置替换默认配置的话就可以使用application.properties或者application.yml(application.yaml)进行配置。
注意配置文件的命名必须是application开头。
优先顺序
在同一级目录下优先级为properties yml yaml
内部配置优先顺序 file:../config/ 当前项目下的/config目录 file:../ 当前项目的根目录 classpath:/config/classpath的/config目录 classpath:/ classpath的根目录
java和resources的文件会被打包到classes的目录
properties和yml实际上会被打包到classpath路径下
即优先级顺序如图 外部配置优先顺序
https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config properties、xml、yml格式比对
properties
server.port8080
server.address127.0.0.1
xml:
serverport8080/portaddress127.0.0.1/address
/server
yml:
server:port:8080address:127.0.0.1 Yamlyml
大小写有区别数据前必须有空格空格数目无所谓作为分隔符否则不识别该数据。缩进的空格数目不重要但是相同层级的元素要左对齐。注释的话快捷键也是ctrl/是#空格。参数引用的话用${}包裹起来即可。 对象(map)键值对的集合
person:name: zhangsan# 行内写法
person: {name: zhangsan}
数组
address:- beijing- shanghai
# 行内写法address: [beijing,shanghai] 纯量单个的、不可再分的值。一般都是字符串
msg1: hello \n world # 单引忽略转义字符
msg2: hello \n world # 双引识别转义字符
读取配置内容
Value
application.yml的对象用到几个就要注入几个。 application.yml中
server:port: 8082name: abcPerson:name: zhangsanage: 20Student:name: ${name} # 占位符表示abc那个name会传输到这里address:- beijing- shanghaimsg1: hello \n world # 不会识别转义字符
msg2: hello \n world # 会识别转义字符 HelloController中
package com.example.springini.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
public class HelloController {Value(${name})//和application.yml的键值对的键要同名与下一行的私有成员名字无关private String nname;Value(${Person.name})private String name2;Value(${Person.age})private int age;Value(${Student.name})private String name3;Value(${address[0]})private String addr;Value(${msg1})private String msg11;Value(${msg2})private String msg22;RequestMapping(/hello2)public String hello2(){System.out.println(nname);System.out.println(name2);System.out.println(name3);System.out.println(age);return hello springboot;}RequestMapping(/hello)public String hello(){System.out.println(addr);System.out.println(msg11);System.out.println(msg22);return hello springboot;}
} AutowiredEnvironment
将application.yml一次性作为一个对象全都注入。 HelloController中
package com.example.springini.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
public class HelloController_by_environment {Autowiredprivate Environment env;RequestMapping(/hello)public void hello(){System.out.println(env.getProperty(name));System.out.println(env.getProperty(Person.name));System.out.println(env.getProperty(address[0]));System.out.println(env.getProperty(msg1));System.out.println(env.getProperty(msg2));}} AutowiredConfigurationProperties
将配置内容与对象相互绑定。 新建Person类中
package com.example.springini;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;Component //表示这个Person类被识别成BeanConfigurationProperties(prefix person)
//如果不指定prefix为person那么它不一定找到配置文件的person底下的两个字段name和age
//而可能去找到单独的两个字段name和age。public class Person {private String name;//命名必须和yml文件中的键值对的键保持一致private int age;public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}
} HelloController中
package com.example.springini.controller;import com.example.springini.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
public class HelloController_by_ConfigurationProperties {Autowiredprivate Person p;RequestMapping(/hello)public void hello(){System.out.println(p.getName() : p.getAge());}} profile动态配置切换
我们在开发Spring Boot应用时通常同一套程序会被安装到不同环境比如开发、测试、生产等。其中数据库地址、服务器端口等等配置都不同如果每次打包时都要修改配置文件那么非常麻烦。profile功能就是来进行动态配置切换的。 配置方式
多profile文件方式
application-dev.properties/yml 开发环境application-test.properties/yml 测试环境application-pro.properties/yml 生产环境
yml多文档方式
在yml中使用----分隔不同配置 激活方式
配置文件
在配置文件中配置spring.profiles.activedev
虞拟机参数
在VM options指定-Dspring.profiles.activedev
命令行参数
java-jar xxx.jar --spring.profiles.activedev