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();
}
}
测试结果如图
网友评论