初識Springboot,創建一個最簡單的示例

開始...

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.yml文件中的變量
application.yml內容

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

...結束

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章