Java SpringBoot 获取 yml properties 自定义配置信息
server :
port : 9090
servlet :
context-path : /app
第一种方法
package com. zhong. demo01. controller ;
import org. springframework. beans. factory. annotation. Value ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ;
@RestController
public class HelloController {
@Value ( "${server.port}" )
String port;
@Value ( "${server.servlet.context-path}" )
String basePath;
@RequestMapping ( "/hello" )
public String hello ( ) {
return "我的启动路径是 http://loaclhost:" + port + basePath + "/hello" + " 我的网页内容是:" + "Hello World !" ;
}
}
第二种方法
package com. zhong. demo01. controller ;
import lombok. Data ;
import org. springframework. boot. context. properties. ConfigurationProperties ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ;
@RestController
@Data
@ConfigurationProperties ( prefix = "server" )
public class HelloController {
String port;
String servlet;
@RequestMapping ( "/hello" )
public String hello ( ) {
return "我的启动路径是 http://loaclhost:" + port + servlet + "/hello" + " 我的网页内容是:" + "Hello World !" ;
}
}
如果使用的是 application.properties
server. port= 9090
server. servlet. context- path= / app