一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Spring-Boot框架初步搭建

Spring-Boot框架初步搭建

2020-08-28 10:52Ryan.Miao Java教程

本篇文章主要介紹了Spring-Boot框架初步搭建,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、簡介

SpringMVC是非常偉大的框架,開源,發(fā)展迅速。優(yōu)秀的設(shè)計必然會劃分、解耦。所以,spring有很多子項目,比如core、context、bean、mvc等。這對知根底的人來說很簡單明了,然而springmvc就是為了傻瓜式的操作而發(fā)明的。對于初學(xué)springmvc的人來說,想要入手就開發(fā)需要拷貝一連串的dependency而不知道這個是干嘛,不知道是不是少了依賴。像我剛接觸springmvc的時候到處百度教程而發(fā)現(xiàn)各有不同,于是復(fù)制了一個又一個代碼卻不能自己設(shè)置,根本原因是不了解各個依賴的包。

Spring-Boot 正是為了解決繁復(fù)的代碼配置而產(chǎn)生的。Spring-Boot 也是基于java-base 開發(fā)的代碼,及不用xml文件配置,所有代碼都由java來完成。還可以加入Groovy的動態(tài)語言執(zhí)行。

 二、搭建一個基本的web-mvc 項目

2.1 Configure environment

  1. java 1.8+
  2. maven 3.3+
  3. spring-boot 1.3.5
  4. idea 15
  5. Thymeleaf 3

2.2 Start

在idea中,選擇new-》maven創(chuàng)建一個空的maven項目,比如名字springboot-test。

2.2.1pom.xml

設(shè)定java版本:

?
1
2
3
4
5
<properties>
 
    <java.version>1.8</java.version>
 
</properties>

添加依賴版本管理dependencyManagement

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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項目依賴

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<build>
 
    <plugins>
 
      <plugin>
 
        <groupId>org.springframework.boot</groupId>
 
        <artifactId>spring-boot-maven-plugin</artifactId>
 
        <configuration>
 
          <fork>true</fork>
 
        </configuration>
 
      </plugin>
 
    </plugins>
 
 
 
</build>

如此,一個簡單的restful的webservice的項目就搭建好了。如果想要支持視圖渲染,即jsp、freeMark、velocity等,添加對應(yīng)的依賴即可。比如,我使用Thymeleaf模板:

?
1
2
3
4
5
6
7
<dependency>
 
      <groupId>org.springframework.boot</groupId>
 
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
 
</dependency>

2.2.2 創(chuàng)建java代碼

如果新建項目的名字是:springboot-test. 則創(chuàng)建包springboot-test/src/main/java/com/test.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
com
 +- example
   +- myproject
     +- Application.java
     |
     +- domain
     |  +- Customer.java
     |  +- CustomerRepository.java
     |
     +- service
     |  +- CustomerService.java
     |
     +- web
       +- CustomerController.java

com.test是我們的基本包名。下面創(chuàng)建配置類com.test.AppConfig。

package com.test;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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)注啟動配置入口,可以發(fā)現(xiàn)通過一個main方法啟動。使用這個注解的類必須放置于最外層包中,因為默認(rèn)掃描這個類以下的包。否則需要自己配置@ComponentScan。 

這樣,配置基本完成了。下面開發(fā)控制層controller:

創(chuàng)建com.test.controller.HelloController。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!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

啟動方式多種,可以啟動main方法,也可以通過命令行啟動:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)

瀏覽器訪問:localhost:8080/index

Spring-Boot框架初步搭建

Spring-Boot框架初步搭建

Spring-Boot框架初步搭建

demo下載路徑:springboot.rar

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/woshimrf/p/5602051.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲免费小视频 | 日韩精品免费一级视频 | 美女被视频网站 | 娇妻被老外疯狂调教 | 午夜国产在线视频 | 午夜宅男网 | 特黄特级高清免费视频毛片 | 2019天天干夜夜操 | 帅老头恋帅老头同性tv | 精品99一区二区三区麻豆 | 国产酒店自拍 | 13日本xxxxxxxxx18 1313午夜精品久久午夜片 | 日韩欧美亚洲天堂 | 大伊香蕉在线精品不卡视频 | caoporn超碰最新地址进入 | 久久综合狠狠综合久久综合88 | 幻女free性俄罗斯第一次摘花 | 日本免费全黄一级裸片视频 | 91香蕉视频导航 | 精品视频免费在线观看 | 亚洲欧美国产另类 | 天天快乐高清在线观看 | 色哟哟在线播放 | 欧美调教打屁股spank视频 | 狠狠色综合久久婷婷色天使 | 国产成人综合精品 | jk制服白丝超短裙流白浆 | 欧美日韩国产一区二区三区在线观看 | 国产成人一区二区三区小说 | 久久久久久免费高清电影 | 亚洲成在人线久久综合 | 青青自拍视频 | 操动漫美女视频 | 成年人免费观看 | 免费观看国产大片资源视频 | 天作谜案免费完整版在线观看 | 欧美特黄一级大片 | 欧美黑人性猛交╳xx╳动态图 | 久久精品成人免费网站 | 美国videos | 午夜AV内射一区二区三区红桃视 |