最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring-Boot框架初步搭建

 更新時間:2017年03月15日 14:09:50   作者:Ryan.Miao  
本篇文章主要介紹了Spring-Boot框架初步搭建,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、簡介

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

Spring-Boot 正是為了解決繁復的代碼配置而產生的。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

設定java版本:

<properties>

    <java.version>1.8</java.version>

</properties> 

添加依賴版本管理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項目依賴

<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> 

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

<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.

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;

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 標注啟動配置入口,可以發(fā)現(xiàn)通過一個main方法啟動。使用這個注解的類必須放置于最外層包中,因為默認掃描這個類以下的包。否則需要自己配置@ComponentScan。 

這樣,配置基本完成了。下面開發(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)建視圖代碼:

視圖默認放在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

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

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

demo下載路徑:springboot_jb51.rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Spring延遲Bean初始化的實現(xiàn)示例

    Spring延遲Bean初始化的實現(xiàn)示例

    延遲初始化也叫做惰性初始化,指不提前初始化Bean,而是只有在真正使用時才創(chuàng)建及初始化Bean,本文主要介紹了Spring延遲Bean初始化的實現(xiàn)示例,感興趣的可以了解一下
    2024-06-06
  • 詳解SpringBoot定時任務功能

    詳解SpringBoot定時任務功能

    這篇文章主要介紹了SpringBoot定時任務功能詳細解析,這次的功能開發(fā)過程中也算是對其內涵的進一步了解,以后遇到定時任務的處理也更清晰,更有效率了,對SpringBoot定時任務相關知識感興趣的朋友一起看看吧
    2022-05-05
  • JAVA根據(jù)ip地址獲取歸屬地的實現(xiàn)方法

    JAVA根據(jù)ip地址獲取歸屬地的實現(xiàn)方法

    本文主要介紹了JAVA根據(jù)ip地址獲取歸屬地的實現(xiàn)方法,要通過Java程序獲取IP地址對應的城市,需要借助第三方的IP地址庫,下面就來介紹一下,感興趣的可以了解一下
    2023-10-10
  • IDEA Spring Boot 自動化構建+部署的實現(xiàn)

    IDEA Spring Boot 自動化構建+部署的實現(xiàn)

    這篇文章主要介紹了IDEA Spring Boot 自動化構建+部署的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • java注解處理器學習在編譯期修改語法樹教程

    java注解處理器學習在編譯期修改語法樹教程

    這篇文章主要為大家介紹了java注解處理器學習在編譯期修改語法樹教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Java應該在哪里判斷List是否為空

    Java應該在哪里判斷List是否為空

    在Java中,我們常用List來存儲數(shù)據(jù),但是我們怎么判斷它是否成功帶來了我們需要的數(shù)據(jù)呢?下面這篇文章主要給大家介紹了關于Java應該在哪里判斷List是否為空的相關資料,需要的朋友可以參考下
    2022-02-02
  • Spring @Transactional注解失效解決方案

    Spring @Transactional注解失效解決方案

    這篇文章主要介紹了Spring @Transactional注解失效解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • spring?boot使用@Async注解解決異步多線程入庫的問題

    spring?boot使用@Async注解解決異步多線程入庫的問題

    最近在寫項目是需要添加異步操作來提高效率,所以下面這篇文章主要給大家介紹了關于spring?boot使用@Async注解解決異步多線程入庫問題的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-05-05
  • Mybatis中SqlMapper配置的擴展與應用詳細介紹(1)

    Mybatis中SqlMapper配置的擴展與應用詳細介紹(1)

    這篇文章主要介紹了Mybatis中SqlMapper配置的擴展與應用(1)的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下
    2016-11-11
  • IDEA實現(xiàn) springmvc的簡單注冊登錄功能的示例代碼

    IDEA實現(xiàn) springmvc的簡單注冊登錄功能的示例代碼

    這篇文章主要介紹了IDEA實現(xiàn) springmvc的簡單注冊登錄功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06

最新評論

隆昌县| 曲松县| 双柏县| 西盟| 工布江达县| 台北县| 班玛县| 永丰县| 宁河县| 博白县| 清涧县| 临海市| 安乡县| 托克逊县| 登封市| 忻城县| 龙陵县| 宁晋县| 万安县| 双城市| 长乐市| 祁阳县| 安义县| 庐江县| 连城县| 依兰县| 镇原县| 岐山县| 达州市| 东阳市| 松桃| 洛隆县| 丰都县| 鄄城县| 大庆市| 凯里市| 邹城市| 嘉兴市| 屏边| 湛江市| 乐东|