开始...
1, 首先创建一个 maven 工程, 引入 springboot 依赖
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!-- 热部署 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <version>2.0.5.RELEASE</version>
- </dependency>
- </dependencies>
2, 编写启动 springboot 的启动类
- @SpringBootApplication
- public class SpringbootApp {
- public static void main(String[] args) {
- SpringApplication.run(SpringbootApp.class, args);
- }
- }
3, 编写一个可访问的 Controller, 该类可以获取 application.YAML 文件中的变量
application.YAML 内容
- server:
- port: 9001
- spring:
- profiles:
- active: Linux
- @Controller
- public class HelloController {
- @Autowired
- ApplicationContext applicationContext;
- @GetMapping(value = "/hello")
- @ResponseBody
- public String hello(){
- return applicationContext.getEnvironment().getProperty("server.port");
- }
- }
4, 在浏览器输入地址 http://localhost:9001/hello
页面显示 9001
... 结束
来源: http://www.bubuko.com/infodetail-3090272.html