第 3 讲 SpringBoot读取配置文件属性

第3讲 SpringBoot读取配置文件属性

1. 获取application.yml文件中的属性

1.1 设置属性:application.yml

test:
  user:
    username: zrblog
    age: 18
    tostring: the age of ${test.user.username} is ${test.user.age}

1.2 编写User类:User.java

  • 实体类添加注解@Configuration,@ConfigurationProperties(prefix = “test.user”)
package com.springboog.ymal.domain;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * @Description:
 * @Author: zrblog
 * @CreateTime: 2018-09-19 23:40
 * @Version:v1.0
 */
@Configuration
@ConfigurationProperties(prefix = "test.user")
public class User {

    private String username;

    private Integer age;

    private String tostring;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getTostring() {
        return tostring;
    }

    public void setTostring(String tostring) {
        this.tostring = tostring;
    }
}

1.3 测试:读取application.yml文件中的属性

package com.springboog.ymal;

import com.springboog.ymal.domain.Student;
import com.springboog.ymal.domain.User;
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)
@SpringBootTest
public class ApplicationTests {

    @Autowired
	private User user;

	@Test
	public void contextLoads() {
	}

     /**
      * @Description: 测试 获取application.yml文件中的属性
      * @Date: 2018/10/9 23:22
      * @Author: zr
      * @param null
      * @Return
      */
	@Test
	public void testApplicationFileAttribute() {
        System.out.println(user.getTostring());
    }
}

2.读取自定义配置文件student.yml文件属性:

2.1 student.yml

test:
  student:
    username: zrblog
    age: 18
    sex:tostring: the age of ${test.user.username} is ${test.user.age}

a

2.2 编写Student类:Student.java

添加以下注解:
@Component
@ConfigurationProperties(prefix = “test.student”)
@PropertySource(“classpath:student.yml”)
@Configuration


package com.springboog.ymal.domain;

import com.sun.org.apache.xpath.internal.operations.String;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author: zrblog
 * @CreateTime: 2018-09-19 23:49
 * @Version:v1.0
 */
@Component
@ConfigurationProperties(prefix = "test.student")
@PropertySource("classpath:student.yml")
@Configuration
public class Student {

    @Value("${username}")
    private String username;

    @Value("${age}")
    private Integer age;

    @Value("${sex}")
    private String sex;

    @Value("${tostring}")
    private String toString;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getToString() {
        return toString;
    }

    public void setToString(String toString) {
        this.toString = toString;
    }
}

2.3 测试:读取student.yml文件中的属性


package com.springboog.ymal;

import com.springboog.ymal.domain.Student;
import com.springboog.ymal.domain.User;
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)
@SpringBootTest
public class ApplicationTests {

    @Autowired
    private Student student;

	@Test
	public void contextLoads() {
	}

     /**
      * @Description: 测试 获取指定文件中的属性
      * @Date: 2018/10/9 23:25
      * @Author: zr
      * @param null
      * @Return 
      */
    @Test
    public void testTargetFileAttribute() {
        System.out.println("testTargetFileAttribute:"+student.getToString());
    }
}

a

目录结构:

a

3.SpringBoot多环境配置

3.1 resources目录下创建application-dev.yml,application-uat.yml,application-prod.yml

b

3.2 配置属性 spring.profiles.active = dev

appliaction.yml

spring:
  profiles:
    active: dev

application-dev.yml

server:
  port: 8081 #配置tomcat访问端口

3.3 测试:访问 http://localhost:8081/student

a

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值