从零开始学习springboot之springboot搭建
博主剛開始實習不久,還是小白一枚,被公司的各種新奇技術搞得眼花繚亂了好久,主要是用springboot和dubbo構建起來的項目。看完之后才知道springboot真的是java開發者的福音啊 話不多說請看正文:
使用Spring Boot創建微服務
一:是什么
微服務是一種架構風格,一個大型復雜軟件應用由一個或多個微服務組成。系統中的各個微服務可被獨立部署,各個微服務之間是松耦合的。每個微服務僅關注于完成一件任務并很好地完成該任務。在所有情況下,每個任務代表著一個小的業務能力。
Spring由于其繁瑣的配置,一度被人成為“配置地獄”,各種XML、Annotation配置,讓人眼花繚亂,而且如果出錯了也很難找出原因。Spring Boot項目就是為了解決配置繁瑣的問題,最大化的實現convention over configuration(約定大于配置)。
Spring Boot的特性有以下幾條:
創建獨立Spring應用程序
嵌入式Tomcat,Jetty容器,無需部署WAR包
簡化Maven及Gradle配置
盡可能的自動化配置Spring
直接植入產品環境下的實用功能,比如度量指標、健康檢查及擴展配置等
無需代碼生成及XML配置
目前Spring Boot的版本為1.2.3,需要Java7及Spring Framework4.1.5以上的支持。
springboot提供的功能還有很多這里就不一一介紹了 具體請看springboot中文文檔
https://qbgbook.gitbooks.io/spring-boot-reference-guide-zh/content/I.%20Spring%20Boot%20Documentation/
二:怎么做
各位看官接下來開始我們的基本配置?
在這里我用的是intellij idea開發工具 以及maven管理工具
1、springboot啟動web工程
這是我的目錄結構
注意這里的webapp必須放在main中與resource和java同級 不然匹配Url時會報404 一開始樓主以為是支持jsp部分配置出錯改了好久 對比了別人的目錄結構才發現 謹記!!!
?
a、編寫pom
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.1.RELEASE</version></parent><dependencies><!--spring-boot-starter-web: MVC,AOP的依賴包....--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--啟動時啟動內置tomcat--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></dependency><!--對jsp支持 --><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId></dependency><!--jstl--><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId></dependency></dependencies>
b、創建主類WarApplication ()
@SpringBootApplication public class WarApplication {public static void main(String[] args) {SpringApplication.run(WarApplication.class);} }
注意 這里的@SpringBootApplication相當于@Configuration@EnableAutoConfiguration@ComponentScan
?c、創建主配置文件application.properties(名字約定俗稱,springboot啟動時會自動讀取resource下該名字的配置文件)
也可以在主類中用@Bean注解的方式注入bean完成整合其他框架 這里就不介紹了 個人覺得用application.properties的方式更加便捷
# 頁面默認前綴目錄 spring.mvc.view.prefix=/WEB-INF/view/
# 響應頁面默認后綴
spring.mvc.view.suffix=.jsp
這里我采用的是properties,也可以使用yml文件 需注意兩者配置規范不同
基本配置已經實現這里開始寫controller和jsp頁面
d、controller
@Controller public class TestController {@RequestMapping("/helloworld")public String test(){return "helloworld";} }
e、jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title></title>
</head>
<body>hello world!!
</body>
</html> 到這里就算是完成了springboot最基本的web helloworld了
接下來我們跑一下:直接啟動主類的main方法即可
成功啟動!
在頁面上請求url helloworld
成功跳轉到helloworld.jsp界面。
?
各位看官如何 是否要比在xml里配置spring、視圖解析器以及配置spring配置文件要方便的多呢,springboot的功能遠不止如此,請聽下回分解----springboot熱部署配置。
?
轉載于:https://www.cnblogs.com/wdfordream/p/6651670.html
總結
以上是生活随笔為你收集整理的从零开始学习springboot之springboot搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 乌镇淡季是什么时候
- 下一篇: teamcity plugin中读取js