Springboot模板整合(二)整合自定义属性和自定义配置文件

目的

将springboot模板整合(一)的基础上,添加自定义属性,自定义配置文件以及多配置文件的关联

设置编码格式

在进行下面操作前,需要设置编码格式,选择File——》settings——》搜索File Encodings
如下图所示
在这里插入图片描述
在上图3,4,5,6 三个位置的编码格式修改为utf-8
注意勾选“Transparent native-to-ascii conversion”前的复选框,这个复选框的意思是"将所有字符转成ascii编码保存(实际上就是转成unicode转义序列)"

乱码问题

如果按照上图设置还是出现乱码问题,那么先删除原有的配置文件,然后重新创建应该就会生效了

多环境配置文件

在springboot项目中默认在resource文件下有一个application.properties文件,而我们的相关配置也一般都写在这个
配置文件上,但是有时我们需要对现有的接口进行测试,springboot为我们提供了一种方法,可以在测试的时候,
引用测试配置文件。
具体的方法如下:
      首先在application.properties文件的同一级目录下,创建一个application-test.properties配置文件,我们可以
将需要测试的数据放到这个文件中。

如何在application.properties文件中引用application-test.properties配置文件?
在application.properties文件中配置如下内容:

#通过springboot的profiles属性实现加载不同的properties文件
spring.profiles.active=test

自定义属性

在application-test.properties文件中配置对应的属性值,如下所示

my.name=黎明
my.age =24

测试自定义属性

创建一个实体类

package com.example.springboot_template01.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 自定义属性的基类
 */
@Component
public class Custom {
    @Value("${my.name}")
    private String studentName;
    @Value("${my.age}")
    private Integer studentAge;

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public Integer getStudentAge() {
        return studentAge;
    }

    public void setStudentAge(Integer studentAge) {
        this.studentAge = studentAge;
    }
}

接口测试类

package com.example.springboot_template01.web;

import com.example.springboot_template01.entity.Custom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 自定义属性测试
 */
@RestController
public class CustomTestController {
    @Autowired
    private Custom custom;
    @Value("${my.name}")
    private String name;
    @GetMapping("/customTest")
    public String resultCustomTest(){
        //两种取自定义属性值的方法,一种是将属性值赋值给对象的属性,如custom;另外一种是直接通过属性赋值,如下面的name
        return custom.getStudentName()+":"+custom.getStudentAge()+":"+name;
    }
}


测试效果

在这里插入图片描述

自定义配置文件

在application.properties同级目录下创建一个test1.properties文件,文件内容如下

test.sex = 男
test.name =John

测试自定义配置文件

实体类

package com.example.springboot_template01.entity;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@PropertySource(value = "classpath:test1.properties",encoding = "utf-8")
@Component
@ConfigurationProperties(prefix = "test")
public class CustomConfig {
    private String sex;
    private String name;

    public String getSex() {
        return sex;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

测试接口类

package com.example.springboot_template01.web;

import com.example.springboot_template01.entity.CustomConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 自定义配置文件测试接口
 */
@RestController
public class CustomerConfigTestController {
    @Autowired
    private CustomConfig customConfig;
    @RequestMapping("/configTest")
    public String resultConfigTest(){
        return customConfig.getName()+":"+customConfig.getSex();
    }
}

测试效果

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值