Spring-Boot框架初步搭建
一、簡(jiǎn)介
SpringMVC是非常偉大的框架,開(kāi)源,發(fā)展迅速。優(yōu)秀的設(shè)計(jì)必然會(huì)劃分、解耦。所以,spring有很多子項(xiàng)目,比如core、context、bean、mvc等。這對(duì)知根底的人來(lái)說(shuō)很簡(jiǎn)單明了,然而springmvc就是為了傻瓜式的操作而發(fā)明的。對(duì)于初學(xué)springmvc的人來(lái)說(shuō),想要入手就開(kāi)發(fā)需要拷貝一連串的dependency而不知道這個(gè)是干嘛,不知道是不是少了依賴(lài)。像我剛接觸springmvc的時(shí)候到處百度教程而發(fā)現(xiàn)各有不同,于是復(fù)制了一個(gè)又一個(gè)代碼卻不能自己設(shè)置,根本原因是不了解各個(gè)依賴(lài)的包。
Spring-Boot 正是為了解決繁復(fù)的代碼配置而產(chǎn)生的。Spring-Boot 也是基于java-base 開(kāi)發(fā)的代碼,及不用xml文件配置,所有代碼都由java來(lái)完成。還可以加入Groovy的動(dòng)態(tài)語(yǔ)言執(zhí)行。
二、搭建一個(gè)基本的web-mvc 項(xiàng)目
2.1 Configure environment
- java 1.8+
- maven 3.3+
- spring-boot 1.3.5
- idea 15
- Thymeleaf 3
2.2 Start
在idea中,選擇new-》maven創(chuàng)建一個(gè)空的maven項(xiàng)目,比如名字springboot-test。
2.2.1pom.xml
設(shè)定java版本:
<properties> <java.version>1.8</java.version> </properties>
添加依賴(lài)版本管理dependencyManagement
<dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.3.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
添加spring-web項(xiàng)目依賴(lài)
<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> <optional>true</optional> </dependency> </dependencies>
添加build-plugin
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build>
如此,一個(gè)簡(jiǎn)單的restful的webservice的項(xiàng)目就搭建好了。如果想要支持視圖渲染,即jsp、freeMark、velocity等,添加對(duì)應(yīng)的依賴(lài)即可。比如,我使用Thymeleaf模板:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2.2.2 創(chuàng)建java代碼
如果新建項(xiàng)目的名字是:springboot-test. 則創(chuàng)建包springboot-test/src/main/java/com/test.
com +- example +- myproject +- Application.java | +- domain | +- Customer.java | +- CustomerRepository.java | +- service | +- CustomerService.java | +- web +- CustomerController.java
com.test是我們的基本包名。下面創(chuàng)建配置類(lèi)com.test.AppConfig。
package com.test;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Created by miaorf on 2016/6/19. */ @SpringBootApplication public class AppConfig { public static void main(String[] args) { SpringApplication.run(AppConfig.class); } }
@SpringBootApplication 標(biāo)注啟動(dòng)配置入口,可以發(fā)現(xiàn)通過(guò)一個(gè)main方法啟動(dòng)。使用這個(gè)注解的類(lèi)必須放置于最外層包中,因?yàn)槟J(rèn)掃描這個(gè)類(lèi)以下的包。否則需要自己配置@ComponentScan。
這樣,配置基本完成了。下面開(kāi)發(fā)控制層controller:
創(chuàng)建com.test.controller.HelloController。
package com.test.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; /** * Created by miaorf on 2016/6/19. */ @Controller public class HelloController { @RequestMapping("/index") public String index(Model model){ model.addAttribute("name","Ryan"); return "index"; } @RequestMapping("/json") @ResponseBody public Map<String,Object> json(){ Map<String,Object> map = new HashMap<String,Object>(); map.put("name","Ryan"); map.put("age","18"); map.put("sex","man"); return map; } }
創(chuàng)建視圖代碼:
視圖默認(rèn)放在springboot-test\src\main\resources\templates**.
所以創(chuàng)建springboot-test\src\main\resources\templates\index.html
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Getting Started: Serving Web Content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p th:text="'Hello, ' + ${name} + '!'" /> </body> </html>
D:\workspace\springboot\springboot-test\src\main\webapp\hello.html
<!DOCTYPE HTML> <html> <head> <title>Getting Started: Serving Web Content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> hello, This is static page. not resolve by server, just the html. </body> </html>
2.2.3 run
啟動(dòng)方式多種,可以啟動(dòng)main方法,也可以通過(guò)命令行啟動(dòng):
D:\temp\springboot-test>mvn spring-boot:run [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.test:springboot-test:jar:1.0-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.springframework.boot:spring-boot-maven-plugin is missing. @ line 49, column 21 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building springboot-test 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> spring-boot-maven-plugin:1.3.5.RELEASE:run (default-cli) > test-compile @ springboot-test >>> [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ springboot-test --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 2 resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ springboot-test --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ springboot-test --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\temp\springboot-test\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ springboot-test --- [INFO] No sources to compile [INFO] [INFO] <<< spring-boot-maven-plugin:1.3.5.RELEASE:run (default-cli) < test-compile @ springboot-test <<< [INFO] [INFO] --- spring-boot-maven-plugin:1.3.5.RELEASE:run (default-cli) @ springboot-test --- [INFO] Attaching agents: [] . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.3.5.RELEASE)
瀏覽器訪(fǎng)問(wèn):localhost:8080/index
demo下載路徑:springboot_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring boot 監(jiān)聽(tīng)容器啟動(dòng)代碼實(shí)例
這篇文章主要介紹了spring boot 監(jiān)聽(tīng)容器啟動(dòng)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10springmvc+shiro+maven 實(shí)現(xiàn)登錄認(rèn)證與權(quán)限授權(quán)管理
Shiro 是一個(gè) Apache 下的一開(kāi)源項(xiàng)目項(xiàng)目,旨在簡(jiǎn)化身份驗(yàn)證和授權(quán),下面通過(guò)實(shí)例代碼給大家分享springmvc+shiro+maven 實(shí)現(xiàn)登錄認(rèn)證與權(quán)限授權(quán)管理,感興趣的朋友一起看看吧2017-09-09Spring Boot配置接口WebMvcConfigurer的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot配置接口WebMvcConfigurer的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11SpringCloud通過(guò)Nacos實(shí)現(xiàn)注冊(cè)中心與遠(yuǎn)程服務(wù)調(diào)用詳解流程
如果不滿(mǎn)足eureka注冊(cè)中心,那么本文記錄的Nacos是不二之選。本文主要記錄Springboot基于Nacos實(shí)現(xiàn)注冊(cè)中心以及遠(yuǎn)程服務(wù)調(diào)用2022-07-07springboot+nginx+https+linux實(shí)現(xiàn)負(fù)載均衡加域名訪(fǎng)問(wèn)簡(jiǎn)單測(cè)試
這篇文章主要介紹了springboot+nginx+https+linux實(shí)現(xiàn)負(fù)載均衡加域名訪(fǎng)問(wèn)簡(jiǎn)單測(cè)試,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05Java線(xiàn)程狀態(tài)及切換、關(guān)閉線(xiàn)程的正確姿勢(shì)分享
這篇文章主要給大家介紹了關(guān)于Java線(xiàn)程狀態(tài)及切換、關(guān)閉線(xiàn)程的正確姿勢(shì),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10AntDesign多環(huán)境配置啟動(dòng)過(guò)程詳解
這篇文章主要為大家介紹了AntDesign多環(huán)境配置啟動(dòng)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11