springboot自定义配置文件的使用

Spring Boot优化了很多配置的方式方法,让简化了配置的环节,让我们能够使用极少的配置就能完成相应的功能,典型的应用便是将数据库连接信息配置在配置文件中,方便数据源以及端口的更换。

A、读取默认配置文件;

springboot的核心配置文件默认为resources根目录下的application.properties或application.yml配置文件;

application.properties文件中配置:

com.landlord.name=lin
com.landlord.sex=男
com.landlord.age=22

方法一:使用@ConfigurationProperties(prefix = "")//设置前缀

创建配置类,在@ConfigurationProperties中设置前缀,属性中不用再添加注解;

package com.landlord.test;

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

@ConfigurationProperties(prefix = "com.landlord")//设置前缀
@Component
public class TestProperties {
    private String name;
    private String sex;
    private String age;

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

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

    public String getAge() {
        return age;
    }

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

在controller中进行测试:

package com.landlord.test.controller;

import com.landlord.test.TestProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private TestProperties testProperties;

    @RequestMapping("/test01")
    public String test01(){
        System.out.println(testProperties.getName() + testProperties.getSex() + testProperties.getAge());
        return "test01";
    }
}

测试结果: 

方法二:使用@Value方式

创建配置类,在属性前添加@Value注解;

用 @Value(“${xxxx}”)注解从配置文件读取值的用法

@Component
public class TestProperties2 {
    @Value("${com.landlord.name}")
    private String name;
    @Value("${com.landlord.sex}")
    private String sex;
    @Value("${com.landlord.age}")
    private String age;

}

测试

package com.landlord.test.controller;

import com.landlord.test.TestProperties;
import com.landlord.test.TestProperties2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private TestProperties testProperties;

    @Autowired
    private TestProperties2 testProperties2;

    @RequestMapping("/test01")
    public String test01(){
        System.out.println(testProperties.getName() + testProperties.getSex() + testProperties.getAge());
        return "test01";
    }

    @RequestMapping("/test02")
    public String test02(){
        System.out.println(testProperties.getName() + testProperties.getSex() + testProperties.getAge());
        return "test02";
    }
}

运行效果

B、读取自定义配置文件;

test.properties文件

com.landlord.name=test
com.landlord.sex=女
com.landlord.age=212

读取自定义配置文件和上述用法一类似,在配置文件类中多添加一个注解

@PropertySource(value = "classpath:test.properties") // 配置文件路径
package com.landlord.test;

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

@ConfigurationProperties(prefix = "com.landlord")//设置前缀
@Component
@PropertySource(value = "classpath:test.properties") // 配置文件路径
public class TestProperties {
    private String name;
    private String sex;
    private String age;

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

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

    public String getAge() {
        return age;
    }

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

测试运行结果: 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值