sonarqube的安裝部署以及整合jenkins
圖片有空整理,帶圖的筆記:https://2e39dcd0.wiz03.com/wapp/pages/view/share/s/0KetPg1bRx7G29XSf327xnnk1SO8yT1vFkcr2Vt3Rq39gk8C
目標
- 能夠理解SpringBoot的設計初衷,開發環境要求
- 能夠搭建SpringBoot的開發工程
- 能夠理解SpringBoot的配置檔案常見配置ApplactionContext.xml
- 導包
- 配置檔案
- api(new物件)
- 能夠使用SpringBoot整合MyBatis,整合Redis進行快取,整合RestTemplate傳送Http請求
- 能夠使用SpringBoot進行簡單程式碼測試
- 能夠打包部署SpringBoot專案
學習今日內容,必備基礎知識:
Spring的物件ioc容器:new ClassPathXMLApplicationContext()、@Value、@Configuration
SpringMVC:@RestController、@RequestMapping
Maven知識:依賴傳遞、依賴管理(BOM,Bill of Material)
、依賴衝突、依賴排除、打包 Mybatis:@Select註解
定時器:cron表示式
一、SpringBoot簡介
當前網際網路後端開發中,JavaEE佔據了主導地位。對JavaEE開發,首選框架是Spring框架。在傳統的Spring開發中,需要使用大量的與業務無關的XML配置才能使Spring框架執行起來,這點備受許多開發者詬病。隨著Spring4.x釋出,Spring已經完全脫離XML,只使用註解就可以執行專案。為了進一步簡化Spring應用的開發,SpringBoot誕生了。它是由Pivotal團隊提供的全新框架,其設計目的是簡化Spring應用的搭建及開發過程,並迎合時下流行的分散式微服務設計思想,越來越多企業在使用SpringBoot。本課程跟隨時代的潮流,帶大家張掌握這門技術。
1.1 設計初衷
- 為Spring開發者提供一種,更快速、體驗更好的Spring應用開發方式。
- 開箱即用,同時也可快速擴充套件
- 嵌入式的Tomcat。
- 絕對沒有冗餘程式碼,無需XML配置。
1.2 核心功能
- 核心能力:Spring容器、日誌、自動配置AutoCongfiguration、Starters
- web應用的能力:MVC、嵌入式Web伺服器
- 資料訪問(持久化):關係型資料庫、非關係型資料庫
- 強大的整合其他技術的能力
- 測試:強悍的應用測試
1.3 開發環境要求
Spring Boot 的2.2.2.RELEASES正式發行版,使用Java8、Java 11或Java 13,對應的Spring版本是5.2.0。構建工具Maven版本要求是3.3及以上,最好是使用Maven的3.5.4版本。
Servlet容器版本:
SpringBoot 支援如下的嵌入式Servlet容器,Spring Boot應用程式最低支援到Servlet 3.1的容器。
Name | Servlet Version |
---|---|
Tomcat 9.0 | 4.0 |
Jetty 9.4 | 3.1 |
Undertow 2.0 | 4.0 |
1.4 Spring怎麼做Web開發?
我們怎麼開發一個web專案:
- web.xml配置:SpringMVC核心控制器(DispatchServlet),Spring容器監聽器,編碼過濾器....
- Spring 配置:包掃描(service、dao),配置資料來源,配置事務....
- SpringMVC配置:包掃描(controller),檢視解析器,註解驅動,攔截器,靜態資源....
- 日誌配置
- 少量業務程式碼
- …
- 部署 Tomcat 除錯,每次測試都需要部署
- …
但是如果用 Spring Boot 呢?
超簡單!無需配置!!無感Tomcat!超迅速搭建功能強大的整套 Web!到底多簡單?入門案例揭曉。
二、SpringBoot快速入門
2.1 Maven搭建SpringBoot工程
Maven搭建SpringBoot工程,實現web的請求響應。瀏覽器訪問在頁面中輸出helloworld
。
- http://127.0.0.1:8080/sayHello
- Hi,my name is SpringBoot!!!
實現步驟:
- 建立Maven工程
- pom.xml檔案中配置起步依賴
- 編寫SpringBoot啟動引導類
- 編寫Controller
- 訪問http://localhost:8080/hello測試
實現過程:
-
建立Maven工程day01_springboot_helloworld
-
pom.xml檔案中配置父座標和web的起步依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!--繼承SpringBoot父POM檔案--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> </parent> <groupId>com.itheima</groupId> <artifactId>day01_springboot_helloword</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!--web 開發的相關依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
-
編寫SpringBoot引導類
@Configuration//配置類 @EnableAutoConfiguration//開啟自動配置 @ComponentScan//包掃描 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class,args); } }
-
編寫三層架構程式碼:Controller
-
controller
@RestController public class HelloController { @RequestMapping("/hello") public String hello(String name){ return "hello world!!!"; } }
-
-
訪問http://localhost:8080/hello測試
2.2 使用IDEA快速建立SpringBoot專案
使用Spring Initializr 方式建立SpringBoot工程。然後實現入門案例的程式碼。
俗稱:基礎部分重複架構程式碼
實現步驟:
-
使用Spring Initializr建立SpringBoot
-
配置專案資訊
-
勾選起步依賴
-
配置檔案儲存路徑地址
-
再次編寫入門案例三層架構程式碼
-
訪問http://localhost:8080/hello介面測試
實現過程:
- 使用建立SpringBoot工程
- 配置專案資訊
- 勾選起步依賴
- 配置檔案儲存路徑地址
- 建立完成後工程目錄結構
- pom檔案介紹
- 編寫入門案例程式碼
- 訪問http://localhost:8080/hello介面測試
2.3 SpringBoot工程熱部署
只需匯入開發者工具依賴座標,即可實現熱部署功能:
<!--spring-boot開發工具jar包,支援熱部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
但還需注意:加入座標之後,如果想要程式碼立即生效,必須在修改程式碼之後進行程式碼構建。預設情況IDEA不會自動構建,需要手動構建。如圖兩處地方均可。
每次手動構建很麻煩?!!還有一種自動構建解決方案,但不建議使用。就是設定Build Project Automatically
。同時開啟Maintenance維護(開啟快捷鍵Shift + Ctrl + Alt + /
),選擇Registry(登錄檔),設定執行時自動編譯。
三、SpringBoot原理分析
3.1 starters的原理
starters是依賴關係的整理和封裝。是一套依賴座標的整合,可以讓匯入應用開發的依賴座標更方便。
利用依賴傳遞的特性:幫你把依賴打包了,starter
搞框架:
- 導包 == starter
- 配置檔案 == AutoConfiguration
- new物件
有了這些Starters,你獲得Spring和其整合的所有技術的一站式服務。無需配置(自動配置)、無需複製貼上依賴座標,一個座標即可完成所有入門級別操作。舉例:Web開發,只需要匯入spring-boot-starter-web
。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
每個Starter包含了當前功能下的許多必備依賴座標,這些依賴座標是專案開發,上線和執行必須的。同時這些依賴也支援依賴傳遞。舉例:spring-boot-starter-web
包含了所有web開發必須的依賴座標
常用的starters有哪些?非常多,一下只列舉部分:
starter為什麼不需要寫版本?
3.2 依賴管理的原理
BOM(Bill of Materials)依賴清單,是由Maven提供的功能,
BOM內定義成套相互相容的jar包版本集合
使用時依賴時,只需依賴該BOM檔案,即可放心的使用清單內的依賴jar包,且無需版本號。
BOM設計初衷:方便維護專案依賴版本升級。
依賴管理(Dependency Management)
-
繼承了
spring-boot-starter-parent
的好處和特點- 預設編譯Java 1.8
- 預設編碼UTF-8
- 通過spring-boot-denpendencies的pom管理所有公共Starter依賴的版本
- spring-boot-denpendencies通過Maven的一個
特性來實現版本管理 - 隨用隨取,不用繼承父類所有的starter依賴。
-
POM檔案中的Maven外掛
<!-- 作用:將一個SpringBoot的工程打包成為可執行的jar包 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
如果想使用父pom檔案中的任何外掛,無需配置即可使用
3.3 自動配置(AutoConfiguration)原理
所有我們要配置的專案Pivotal團隊的開發人員,幫我們寫好了,怎麼實現的,主要是通過@Configuration實現
SpringBoot採用約定大於配置設計思想,將所有可能遇到的配置資訊提前配置好,寫在自動配置的jar包中。每個Starter基本都會有對應的自動配置。
SpringBoot幫我們將配置資訊寫好,存放在一個jar包中:spring-boot-autoconfigure-2.1.11.RELEASE.jar
jar包裡,存放的都是配置類,讓配置類生效的"規則類"
自動配置的值在哪裡?
自動配置的值怎麼才能生效?
檢視啟動類註解@SpringBootApplication
追蹤步驟:
- @EnableAutoConfiguration
- @Import({AutoConfigurationImportSelector.class})
- spring.factories
- org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration
- @EnableConfigurationProperties({ServerProperties.class})
- private final ServerProperties.Tomcat tomcat = new ServerProperties.Tomcat();
有了自動配置,那麼基本全部採用預設配置。當然也可以更改預設配置,怎麼改?
官網的自動配置的地址: https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/common-application-properties.html
三個原理分析小節:
- Starter:是一套依賴關係的整理和封裝
- 讓我們更加專注於業務開發,無需關心依賴匯入,依賴衝突,及依賴的版本
- 在pom檔案匯入starter既可使用對應的功能
- 依賴管理:依賴管理是對依賴座標的抽取和複用,統一管理依賴座標的版本。
- 實現了依賴座標的版本管理
- starter隨用隨取
- 避免了繼承所有父類starter的依賴的臃腫
- 避免了記憶所有starter的麻煩。
- 自動配置:預先寫入配置類,封裝到AutoConfiguration的jar包中,按需求載入配置資訊。
- 基於約定大於配置的設計思想
- 極大的降低了Spring應用配置的複雜度
- 程式碼實現原理:@SpringBootApplication-->@EnableAutoConfiguration-->@AutoConfigurationPackage(spring-boot-autoconfigure-2.1.7.RELEASE.jar)
- 原理的核心在於:spring-boot-autoconfigure-2.1.7.RELEASE.jar包
四、SpringBoot的配置檔案
三種配置檔案:
- properties:參考資料中配置
#
server.port=8080
server.address=127.0.0.1
- xml (Markup Language):
<server>
<port>8080</port>
<address>127.0.0.1</address>
</server>
- yml/yaml:
server:
port: 8080
address: 127.0.0.1
我們知道SpringBoot是約定大於配置的,所以很多配置都有預設值。如果想修改預設配置,可以使用application.properties或application.yml(application.yaml)自定義配置。SpringBoot預設從Resource目錄載入自定義配置檔案。application.properties是鍵值對型別(一直在用)。application.yml是SpringBoot中一種新的配置檔案方式。
4.1 application.yml配置檔案
YML檔案格式是YAML(YAML Aint Markup Language)編寫的檔案格式。可以直觀被電腦識別的格式。容易閱讀,容易與指令碼語言互動。可以支援各種程式語言(C/C++、Ruby、Python、Java、Perl、C#、PHP)。以資料為核心,比XML更簡潔。副檔名為.yml或.yaml;
官網地址:https://yaml.org/
YAML: YAML Ain't Markup Language
What It Is: YAML is a human friendly data serialization standard for all programming languages
4.2 配置檔案語法
- 大小寫敏感
- 資料值前邊必須有空格,作為分隔符
- 使用縮排表示層級關係
- 縮排不允許使用tab,只允許空格
- 縮排的空格數不重要,只要相同層級的元素左對齊即可
- ‘#’表示註釋,從這個字元一直到行尾,都會被解析器忽略。
- 陣列和集合使用 “- ”表示陣列每個元素
server:
port: 8080
address: 127.0.0.1
name: abc
YAML案例:
單個:
# 單引號忽略轉義字元
message1: 'hello \n world'
# 雙引號識別轉義字元
message2: "hello \n world"
物件(map):鍵值對的集合:
person:
name: lisi
age: 31
addr: beijing
# 行內寫法
person: {name: haohao, age: 31, addr: beijing}
陣列:一組按次序排列的值
city:
- beijing
- shanghai
- guangzhou
# 行內寫法
city: [beijing,shanghai,guangzhou]
集合:
#集合中的元素是物件形式
animals:
- name: dog
age: 2
- name: tomcat
age: 3
- name: pig
age: 5
配置引用:
name: lisi
person:
name: ${name}
配置隨機數:
# 隨機字串
my.secret: ${random.value}
# 隨機數
my.number: ${random.int}
# 隨機數小於10
my.number.less.than.ten: ${random.int(10)}
# 隨機數範圍在1024-65536之間
my.number.in.range: ${random.int[1024,65536]}
融合所有寫法:
person:
name: haohao
age: 31
addr: beijing
city:
- beijing
- shanghai
- guangzhou
animals:
- name: dog
age: 2
- name: tomcat
age: 3
- name: pig
age: 5
4.3 SpringBoot配置資訊的查詢
修改配置時,配置專案查詢方式
第一種:
第二種:
常用配置:
# QUARTZ SCHEDULER (QuartzProperties)
spring.quartz.jdbc.initialize-schema=embedded # Database schema initialization mode.
spring.quartz.jdbc.schema=classpath:org/quartz/impl/jdbcjobstore/tables_@@platform@@. sql # Path to the SQL file to use to initialize the database schema.
spring.quartz.job-store-type=memory # Quartz job store type.
spring.quartz.properties.*= # Additional Quartz Scheduler properties.
# ----------------------------------------
# WEB PROPERTIES
# ----------------------------------------
# EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.port=8080 # Server HTTP port. server.servlet.context-path= # Context path of the application. server.servlet.path=/ # Path of the main dispatcher servlet.
# HTTP encoding (HttpEncodingProperties)
spring.http.encoding.charset=UTF-8 # Charset of HTTP requests and responses. Added to the "Content-Type" header if not set explicitly.
# JACKSON (JacksonProperties)
spring.jackson.date-format= # Date format string or a fully-qualified date format class name. For instance, `yyyy-MM-dd HH:mm:ss`.
可以通過修改application.properties或者application.yml來修改SpringBoot的預設配置
例如:
application.properties檔案
# 常見配置專案
# 埠
server.port=8080
# 專案的contentpath路徑
server.servlet.context-path=/demo
# 開啟debug模式
debug=true
# 配置日誌級別,為debug
logging.level.com.example=debug
application.yml檔案
server:
port: 8888
servlet:
# 應用的
context-path: /demo
擴充套件點
- properties檔案轉換為yml檔案: https://www.toyaml.com/index.html
4.4 配置檔案屬性注入Bean
1、使用註解@Value對映
@value註解將配置檔案的值對映到Spring管理的Bean屬性值
2、使用註解@ConfigurationProperties對映
通過註解@ConfigurationProperties(prefix=''配置檔案中的key的字首")可以將配置檔案中的配置自動與實體進行對映。
使用@ConfigurationProperties方式必須提供Setter方法,使用@Value註解不需要Setter方法。
3、使用Environment物件獲取
注入Environment物件,即可從物件中獲取配置檔案中的值
五、SpringBoot與其他技術整合
5.1 整合MyBatis
使用SpringBoot整合MyBatis,完成查詢所有功能。
實現步驟:
- 建立SpringBoot工程,勾選依賴座標
- 建立User表、建立實體User
- 編寫三層架構:Mapper、Service、controller,編寫查詢所有的方法findAll()
- 編寫Mapper介面中的方法findAll()的SQL語句
- 配置檔案:資料庫連線資訊
- 訪問測試地址http://localhost:8080/queryUsers
實現過程:
-
建立SpringBoot工程,day01_springboot_mybatis;
勾選依賴座標
-
建立User表—>建立實體UserBean
-
建立表
-- ---------------------------- -- Table structure for `user` -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) DEFAULT NULL, `password` varchar(50) DEFAULT NULL, `name` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '張三'); INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');
-
建立實體
public class User { private Integer id; private String username;//使用者名稱 private String password;//密碼 private String name;//姓名 //getter setter... //toString }
-
-
編寫使用者Controller,編寫UserService
-
UserController
@RestController @RequestMapping("user") public class UserController { @Autowired private UserService userService; @RequestMapping("findAll") public List<User> findAll(){ return userService.findAll(); }
-
UserService
public interface UserService { List<User> findAll(); }
-
UserServiceImpl
@Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> findAll() { return userMapper.findAll(); } }
-
-
編寫Mapper:使用@Mapper標記該類是一個Mapper介面,可以被SpringBoot自動掃描
@Mapper public interface UserMapper { @Select("select * from user;") List<User> findAll(); }
-
還有一種辦法,可以在配置類中配置Mapper掃描
/** * @MapperScan(basePackages = "com.itheima.mapper") * 掃描指定包下的所有Mapper介面,將動態代理的實現類物件注入Spring容器中 * basePackages屬性:指定掃描的包路徑地址 * 作用相當於: * <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> * <property name="basePackage" value="com.itheima.dao"/> * </bean> */ @SpringBootApplication @MapperScan(basePackages = "com.itheima.mapper") public class SpringbootMybatisApplication { public static void main(String[] args) { SpringApplication.run(SpringbootMybatisApplication.class, args); } }
-
-
在application.properties中新增資料庫連線資訊
# DB 配置 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.password=root spring.datasource.username=root # spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- 資料庫連線地址後加
?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
,不然會報錯
- 資料庫連線地址後加
5.2 整合Spring Data Redis
SpringBoot整合了Redis之後,做使用者資料查詢快取。
實現步驟:
- 新增Redis起步依賴
- 在application.properties中配置redis埠、地址
- 注入RedisTemplate操作Redis快取查詢所有使用者資料
- 測試快取
實現過程:
-
新增Redis起步依賴
<!--spring data redis 依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
-
配置Redis連線資訊
# Redis 配置(不填也是可以的) spring.redis.host=localhost spring.redis.port=6379
-
注入RedisTemplate測試Redis操作
@Test public void testRedis() throws JsonProcessingException { String users = (String) redisTemplate.boundValueOps("user.findAll").get(); if (users == null) { List<User> userList = userMapper.queryUserList(); ObjectMapper jsonFormat = new ObjectMapper(); users = jsonFormat.writeValueAsString(userList); redisTemplate.boundValueOps("user.findAll").set(users); System.out.println("==============從資料庫中獲取使用者資料==================="); }else { System.out.println("==============從Redis快取中獲取使用者資料==================="); } System.out.println(users); }
5.3 整合定時器
需求:使用SpringBoot開發定時器,每隔5秒輸出一個當前時間。
實現步驟:
-
開啟定時器註解
@SpringBootApplication @EnableScheduling//開啟定時器 public class Day01SpringbootIntergrationApplication { public static void main(String[] args) { SpringApplication.run(Day01SpringbootIntergrationApplication.class, args); } }
-
配置定時器方法
@Component public class TimerUtil { @Scheduled(initialDelay = 1000,fixedRate = 1000) public void mytask(){ System.out.println(LocalDateTime.now()); } }
-
測試定時器。
5.4 傳送HTTP請求
1、Spring的RestTemplate
- RestTemplate是Rest的HTTP客戶端模板工具類
- 對基於Http的客戶端進行封裝
- 實現物件與JSON的序列化與反序列化
- 不限定客戶端型別,目前常用的3種客戶端都支援:HttpClient、OKHttp、JDK原生URLConnection(預設方式)
2、RestTemplate案例
目標需求:傳送Http請求
實現步驟:
- 建立一個springboot的工程
- 配置RestTemplate的物件Bean到Spring容器中
- 在測試類中用@Autowired注入Spring容器中的RestTemplate物件
- 通過RestTemplate物件的getForObject傳送請求
- 執行測試類的測試方法
實現過程:
-
建立一個springboot的工程,勾選Web的Starter
勾選web開發的Starter
-
在專案啟動類位置中註冊一個RestTemplate物件
@Configuration public class MyConfiguration { @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } }
-
在測試類ApplicationTests中
@Autowired
注入RestTemplate -
通過RestTemplate的getForObject()方法,傳遞url地址及實體類的位元組碼
@RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests { @Autowired private RestTemplate restTemplate; @Test public void testREST() { String url = "http://baidu.com"; String json = restTemplate.getForObject(url, String.class); System.out.println(json); } }
- RestTemplate會自動發起請求,接收響應
- 並且幫我們對響應結果進行反序列化
-
執行測試類中的testREST方法;
5.5 擴充套件瞭解:除此之外還可以整合什麼?
- 整合 MongoDB
- 整合 ElasticSearch
- 整合 Memcached
- 整合郵件服務:普通郵件、模板郵件、驗證碼、帶Html的郵件
- 整合RabbitMQ訊息中介軟體
- 整合Freemarker或者Thymeleaf
六 、SpringBoot如何程式碼測試
目標:SpringBoot整合JUnit測試功能,進行查詢使用者介面測試
實現步驟:
-
新增Junit起步依賴(預設就有)
<!--spring boot測試依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
-
編寫測試類:
-
SpringRunner繼承SpringJUnit4ClassRunner,使用哪一個Spring提供的測試引擎都可以。指定執行測試的引擎
-
@SpringBootTest的屬性值指的是引導類的位元組碼物件
-
注意:最新版的2.2.0.RELEASE中,springboot測試類不再需要@Runwith的註解
@RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests { @Autowired private UserMapper userMapper; @Test public void test() { List<User> users = userMapper.queryUserList(); System.out.println(users); } }
-
-
控制檯列印資訊
七、Spring Boot 如何打包部署
啟動方式有兩種,一種是打成jar直接執行,另一種是打包成war包放到Tomcat服務下,啟動Tomcat。
6.1 打成Jar包部署
執行maven打包命令或者使用IDEA的Maven工具打包
## 移動至專案根目錄,與pom.xml同級
mvn clean package
## 或者執行下面的命令 排除測試程式碼後進行打包
mvn clean package -Dmaven.test.skip=true
需要注意專案pom.xml檔案中的打包型別
<packaging>jar</packaging>
啟動命令:啟動之前先檢查自己的pom.xml檔案中是否有springboot的maven外掛
java -jar target/day01_springboot_demo01-1.0-SNAPSHOT.jar
啟動命令的時候配置jvm引數也是可以的。然後檢視一下Java的引數配置結果
java -Xmx80m -Xms20m -jar target/day01_springboot_demo01-1.0-SNAPSHOT.jar
6.2 打成war包部署
- 執行maven打包命令或者使用IDEA的Maven工具打包,需要修改pom.xml檔案中的打包型別。
<packaging>war</packaging>
- 註冊啟動類
- 建立 ServletInitializer.java,繼承 SpringBootServletInitializer ,覆蓋 configure(),把啟動類 Application 註冊進去。外部 Web 應用伺服器構建 Web Application Context 的時候,會把啟動類新增進去。
//WEB-INF/web.xml
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DemoApplication.class);
}
}
- 然後執行打包操作。同6.1 小節打包是一樣的
- 拷貝到Tomcat的webapp下,啟動Tomcat訪問即可
- 因為訪問地址不再是根目錄了,所有路徑中需要加入專案名稱:http://localhost:8080/day01_springboot_demo01-1.0-SNAPSHOT/hello
總結:
- 能夠理解SpringBoot的設計初衷,開發環境要求
- 能夠搭建SpringBoot的開發工程
- 能夠理解SpringBoot的配置檔案常見配置
- 能夠使用SpringBoot整合MyBatis,整合Redis進行快取,整合RestTemplate傳送Http請求
- 能夠使用SpringBoot進行簡單程式碼測試
- 能夠打包部署SpringBoot專案