Spring Boot學習筆記1——搭建一個簡單的Spring Boot專案
阿新 • • 發佈:2019-01-05
1.建立一個Maven專案匯入相應的依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xx</groupId> <artifactId>springboot-sgg</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 匯入SpringBoot的父依賴,由他來管理依賴版本 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath /> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
2.建立一個主程式類,這裡需要注意我的這個主程式類是放在com.xx這個包下的,會自動掃描com.xx下的所有包
package com.xx; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /* * @SpringBootApplication用於標註一個主程式類,其底層@Import註解會將主配置類所在包下的所有子包裡面的所有元件掃描到Spring容器中 */ @SpringBootApplication public class HelloWorldMainApplication { public static void main(String[] args) { //Spring啟動入口 SpringApplication.run(HelloWorldMainApplication.class, args); } }
3.寫一個Controller用來測試
package com.xx.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @ResponseBody @RequestMapping("/hello") public String hello() { return "HelloWorld!"; } }
4.執行主程式類,輸入http://localhost:8080/hello