美文网首页
SpringBoot自定义配置

SpringBoot自定义配置

作者: MHLEVEL | 来源:发表于2020-08-20 20:38 被阅读0次

点击阅读原作者的原文

1、 添加配置文件


test.properties

hello.name
hello.time

2、 创建配置类


TestConfiguration.java

package com.mhlevel.Config;

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

@Configuration
@PropertySource("classpath:test.properties")
@ConfigurationProperties(prefix = "hello")
public class TestConfiguration {

    private String name;

    private String time;

    public String getName() {
        return name;
    }

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

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

3、 测试配置


TestAction.java

package com.mhlevel.controller;

import com.mhlevel.Config.TestConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value="test")
public class TestAction {

    @Autowired
    private TestConfiguration testConfiguration;

    @RequestMapping(value = "config")
    public String test(){
        return testConfiguration.getName() + " <br/> " + testConfiguration.getTime();
    }
}

测试结果如图

相关文章

网友评论

      本文标题:SpringBoot自定义配置

      本文链接:https://www.haomeiwen.com/subject/spbejktx.html