SpringBoot(五) application配置文件

Application.pro[erties文件

这个文件就是用来修改默认配置信息的配置文件。

application.properties/application.yml

application.properties   【键值对】
          spring.datasource.username=root


    application.yml   【阶梯状】
          spring:
              datasource:
                    username: root

1.如何查找修改的默认配置信息的键名称

spring-boot-autoconfigrue:2.6.4.jar ===> META-INF ====>spring.factories ===> #Auto Configure ===>@EnableConfigurationProperties({DataSourceProperties.class})===>DataSourceProperties.class,找到下面的类

 @ConfigurationProperties(prefix = "spring.datasource")

 在application.properties / application.yml文件中修改spring.datasource.url=自己的新数据值。

2.application.yml文件中设置其他类型的值

先导入需要的依赖

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

2.1设置数字、布尔值。

stufn:
    inttest : 13
    dtest : 12.34

2.2设置字符串

 # String 可以不写双引号
    # str: 你好
    # String 可以写双引号,转译字符会执行
    # str : "hello\n word"
    # String 可以写单引号,不会执行转译字符
    str : 'hello\n word'

2.3设置Object

# Object 对象
    # testBean:
    #   id : 1002
    #   title : "双11大甩卖~"
    testBean: { 'id': '李四','title': '凤女'}

 2.4设置数组、set、list

 # array 类型
    arr: [10,8,12,3,8,4,6,1,3,0]
    # list 类型
    liststr: [10,8,12,3,8,4,6,1,3,0]
    # set 类型
    setlist : [25,36]

2.5设置map集合

# map 类型
    # map:
    #    name : 李四
    #    fn : 凤女
    # map 类型
    map: { 'name': '李四','fn': '凤女'}

Test测试

代码实例: 

@Test
    void contextLoads() {
        System.out.println("int类型的值-->" + ynm.getInttest());
        System.out.println("double类型的值-->" + ynm.getDtest());
        System.out.println("String类型-->" + ynm.getStr());
        System.out.println("Boolean类型--> " + ynm.isAnfn());
        System.out.println("**********数组类型*************");
        for (int i : ynm.getArr()) {
            System.out.print(i + "\t");
        }
        System.out.println();
        System.out.println("**********list集合类型*************");
        for (String s : ynm.getListstr()) {
            System.out.print(s + "\t");
        }

        System.out.println();
        System.out.println("**********set集合类型*************");
        for (String s : ynm.getSetlist()) {
            System.out.print(s + "\t");
        }
        System.out.println();
        System.out.println("**********map集合类型*************");
        Map<String, String> map = ynm.getMap();
        for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
            System.out.println(stringStringEntry.getKey()+ " ==>" +stringStringEntry.getValue());

        }

    }

运行结果: 

 3.application.properties文件

student.stuint = 1002
student.studouble = 12.5
student.stuboolean = true
#字符串--默认不要引号
student.stustring = zhangsan
#字符串数据值中有转义字符
#希望转义字符运行,使用""
#student.stustring = "zhangsan\nlisi"
#不希望转义字符运行,使用‘’
#student.stustring = 'zhangsan\nlisi'
#对象类型的配置
student.person-bean.perid = 2222
student.person-bean.pername = testname
#List集合类型的配置
student.stulist = javase,javaee,javame
#Set集合类型的配置
student.stuset = zhangsan,lisi,wangwu
#Map集合类型的配置
student.stumap.myname = testzhangsan
student.stumap.myage = 23

4.@Value将数据值与javabean类中的成员变量绑定

1.直接复制

 2.导入外部文件赋值

外部的ptest.yml文件

cid : 13
dtest : 12.5
btest : false
stest : '你好呀'
list : 15,26,13,25,16
set : 15,26,358,15,13
stumap: { "bot": "123456","myname" : "testzhangsan", "myage" : "23" }

通过@PropertySource 注解引入外部资源文件 

@Component
@PropertySource("classpath:ptest.yml")
public class ChildrenBean {
    @Value("${cid}")
    private int cid;
    @Value("${dtest}")
    private double dtest;
    @Value("${btest}")
    private boolean btest;
    @Value("${stest}")
    private String stest;
    @Value("${list}")
    private List<String> list;
    @Value("${set}")
    private Set<String> set;
    @Value("#{${stumap:{}}}")
    private Map<String,String> map;
    public int getCid() {
        return cid;
    }

运行结果:

 #{表达式}与 ${表达式}
@Value("#{}")  Spring 表达式语言(简称SpEL) 可以进行数据运算 / 类型转换
@Value("${xxxxx}") 单一的从配置文件中引用数据值,读取数据值 ,不会进行数据运算 / 类型转换。

@ImportResource(value = "classpath:userbean.xml")--引入spring类型的XML配置文件

在resources目录下定义personBean.xml

<?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="userBean" class="com.example.demo.bean.PersonBean">
        <property name="name" value="zhangsan"></property>
        <property name="age" value="18"></property>
        <property name="address" value="西安"></property>
        <property name="tId" value="01"></property>
    </bean>
</beans>

在主类中引入该文件

@SpringBootApplication
@ImportResource("classpath:personBean.xml")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

 测试类中执行

@SpringBootTest
class DemoApplicationTests {
    @Autowired
    private PersonBean personBean;
    @Test
    void contextLoads() {
        System.out.println(personBean);
        //PersonBean{tId=1, name='zhangsan', age=18, address='西安'}
    }
}

5.Profiles


1.Profifile文件就是用来配置在不同环境下的配置数据。
2.因为在不同的环境下配置文件中配置的运行环境的数据是不同的,所以我们就需要灵活的在不同的运行环境下切换成对应的运行环境的数据,此时我们将不同的运行环境数据,配置到不同的配置文件中,通过在主配置文件application.properties中的spring.profiles.active属性完成切换。

application-dev.properties【开发环境配置】
server.port=8080
application-prod.properties【生产环境配置】
server.port=9090
application.properties 【主配置】
spring.profiles.active=prod 【指定使用生产环境配置】

测试:http://localhost:9090/testInfo

application.properties 【主配置】
spring.profiles.active=dev 【指定使用开发环境配置】

测试:http://localhost:8080/testInfo


测试.yml配置
application-devyml.yml【开发环境配置】
server: 
    port: 8080
application-prodyml.yml【生产环境配置】
server: 
    port: 9090
application.yml 【主配置】
spring: 
    profiles: 
        active: prodyml 【指定使用生产环境配置】

http://localhost:9090/testInfo

spring: 
    profiles: 
        active: devyml 【指定使用开发环境配置】

http://localhost:8080/testInfo


主配置文件加载位置
spring boot 启动会扫描以下位置的application.properties或者 application.yml文件作为Spring boot的默认配置文件 
– 项目根目录/config/ 
– 项目根目录/ 
– resource/config/ 
– resource:/  
以上是按照优先级从高到低的顺序,所有位置的文件都会被加载,高优先级配置内容会覆盖低优先级配置内容。
SpringBoot会从这四个位置全部加载主配置文件;互补配置
项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;指定配置文件和默认加载的这些配置文件共同起作用形成互补配置; 
java -jar testspringboot02-0.0.1-SNAPSHOT.jar --spring.confifig.location=F:/application.properties

外部配置加载顺序 
Spring Boot 支持多种外部配置方式 
1. 命令行参数 
2. 来自java:comp/env的JNDI属性 
3. Java系统属性(System.getProperties()) 
4. 操作系统环境变量 
5. RandomValuePropertySource配置的random.*属性值 
6. jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件 
7. jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件 
8. jar包外部的application.properties或application.yml(不带spring.profile)配置文件 
9. jar包内部的application.properties或application.yml(不带spring.profile)配置文件 
优先加载带profifile,再来加载不带profifile,由jar包外向jar包内进行寻找
10. @Configuration注解类上的@PropertySource 
11. 通过SpringApplication.setDefaultProperties指定的默认属性

无奈源于不够强大

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值