读取appliaction.properties里面的端口数据
@Value方式
@RestController
public class getText {
//@value方式读取文件
@Value("${server.port}")
private String port;
@GetMapping("getPort")
public String getPort(){
return port;
}
}
使用ConfigurationProperties 配合类来实现
package com.example.spring.domain;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "text")
@Component
public class ServerSettings {
private String name;
private String domain;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
}
//读取项目域名
@Autowired
private ServerSettings serverSettings;
@GetMapping("getServer")
public Object getServer(){
return serverSettings;
}