溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

初識Springboot,創(chuàng)建一個最簡單的示例

發(fā)布時間:2020-07-12 00:21:00 來源:網(wǎng)絡(luò) 閱讀:476 作者:戀上程序員 欄目:軟件技術(shù)

開始...

1、首先創(chuàng)建一個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內(nèi)容

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

...結(jié)束

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI