*注:此文章謹以記錄學習過程村怪,分享學習心得!
剛剛開始了解SpringBoot框架盛末,覺得很好用,覺得很有必要深入學習一下該框架否淤,現(xiàn)在就來創(chuàng)建一個SpringBoot項目:
1悄但、在IDEA上新建一個Project,選擇Spring Initializr,
Project SDK 選擇安裝的JDK石抡;
Choose Initializr Service URL 選擇默認(Default:https://start.spring.io)
點擊Next
2檐嚣、進行項目配置
設置項目數(shù)組(group),項目標識(Artifact)啰扛,Type選擇一個Maven Project 表示是一個maven項目
Version:項目版本號
Name:項目名稱
Description:項目描述
Package:項目包名
點擊Next 下一步
3嚎京、選擇項目模板
我們來選擇創(chuàng)建一個Web項目
選擇Spring Boot版本
4、設置項目名稱和項目路徑
設置完項目路徑隐解,和項目名稱后鞍帝,點擊FInish,創(chuàng)建項目完成煞茫,需要進行項目構建帕涌,等一小會即可完成。
5续徽、創(chuàng)建完成蚓曼,我們刪除.mvn文件夾,mvnw文件和 mvnw.cmd文件
6钦扭、我們來看一下maven配置的pom.xml文件辟躏,里面包含了SpringBoot項目運行所需的版本庫
SpringBoot運行所需庫為:
<!-- SpringBoot項目的基礎庫文件-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- SpringBoot項目的基礎庫文件-->
<dependencies>
<!-- web項目庫-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 測試所需庫-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
7、創(chuàng)建一個HelloService
package com.example.springbootdemo.service;
import org.springframework.stereotype.Service;
@Service
public interface HelloService {
String sayHello();
}
8土全、創(chuàng)建HelloService的實現(xiàn)類HelloServiceImpl捎琐,實現(xiàn)sayHello()方法会涎,返回"Hello World!"
package com.example.springbootdemo.service.impl;
import com.example.springbootdemo.service.HelloService;
import org.springframework.stereotype.Component;
@Component
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello() {
return "Hello World!";
}
}
9、創(chuàng)建HelloController瑞凑,調(diào)用HelloService實現(xiàn)類末秃,打印"Hello World!"到瀏覽器
package com.example.springbootdemo.controller;
import com.example.springbootdemo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/")
public class HelloController {
@Autowired
private HelloService helloService;
@RequestMapping("/hello")
@ResponseBody
public String helloWorld(){
return helloService.sayHello();
}
}
10、見證奇跡的時刻籽御,我們來運行一下所建項目练慕,看能不能跟我們預期一樣,在瀏覽器輸入訪問地址http://localhost:8080/hello
就可以看到Hello World!
至此技掏,學習創(chuàng)建一個SpringBoot項目就完成了铃将。
查看源碼