*注:此文章謹以記錄學習過程,分享學習心得!
剛剛開始了解springboot框架,覺得很好用,覺得很有必要深入學習一下該框架,現在就來創建一個springboot項目:
1、在idea上新建一個project,選擇spring initializr,
project sdk 選擇安裝的jdk;
choose initializr service url 選擇默認(Default:https://start.spring.io )
選擇項目模板
點擊next
2、進行項目配置
設置項目數組(group),項目標識(artifact),type選擇一個maven project 表示是一個maven項目
version:項目版本號
name:項目名稱
description:項目描述
package:項目包名
項目配置
點擊next 下一步
3、選擇項目模板
我們來選擇創建一個web項目
選擇spring boot版本
選擇項目模板
4、設置項目名稱和項目路徑
設置項目名稱和項目路徑
設置完項目路徑,和項目名稱后,點擊finish,創建項目完成,需要進行項目構建,等一小會即可完成。
5、創建完成,我們刪除.mvn文件夾,mvnw文件和 mvnw.cmd文件
刪除文件
6、我們來看一下maven配置的pom.xml文件,里面包含了springboot項目運行所需的版本庫
pom.xml
springboot運行所需庫為:
1
2
3
4
5
6
7
|
<!-- 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> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!-- 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、創建一個helloservice
1
2
3
4
5
6
|
package com.example.springbootdemo.service; import org.springframework.stereotype.service; @service public interface helloservice { string sayhello(); } |
8、創建helloservice的實現類helloserviceimpl,實現sayhello()方法,返回"hello world!"
1
2
3
4
5
6
7
8
9
10
11
|
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、創建hellocontroller,調用helloservice實現類,打印"hello world!"到瀏覽器
1
2
3
4
5
6
7
8
9
10
11
|
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!" ; } } |
10、見證奇跡的時刻,我們來運行一下所建項目,看能不能跟我們預期一樣,在瀏覽器輸入訪問地址 http://localhost:8080/hello
就可以看到hello world!
至此,學習創建一個springboot項目就完成了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jianshu.com/p/6e096aa974fd