本文包括:html
1. springboot 基本pom.xml配置
<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> <!--項目或者組織的惟一標誌--> <groupId>wqz.zoom</groupId> <!--項目名--> <artifactId>springbootdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath></relativePath> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project>
須要不一樣的開發方向,按照下方jar包介紹,添加依賴便可 java
springboot 依賴包詳解:
轉自 https://blog.csdn.net/chen15369337607/article/details/78445773web
<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zlc</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>demo</name><description>demoprojectforSpringBoot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/><!--lookupparentfromrepository--></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><!--從父依賴那繼承版本號--><!--生產準備的特性,用於幫你監控和管理應用--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--對高級消息隊列協議的支持,經過SpringBoot-rabbit實現--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><!--對面向切面變成的支持,經過spring-aop和AspectJ--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><!--對Springbatch的支持,包括HSQLDB數據庫--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-batch</artifactId></dependency><!--對MongoDBNOSQL數據庫的支持,包括spring-data-mongodb--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId></dependency><!--經過對REST暴露Springdata倉庫的支持,經過Spring-data-rest-webmvc實現--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-rest</artifactId></dependency><!--對java持久化API的支持,包括Spring-data-jpa,spring-orm和Hibernate--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-solr</artifactId></dependency><!--對FreeMarker模板引擎的支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency><!--對基於HATEOAS的RESTful服務的支持,經過Spring-hateoas;--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-hateoas</artifactId></dependency><!--對普通Spring-integration模塊的支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-integration</artifactId></dependency><!--對JDBC數據庫的支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!--對JerseyRESTfulWeb服務框架的支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jersey</artifactId></dependency><!--對JTA分佈式事務的支持,經過Atomikos--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jta-atomikos</artifactId></dependency><!--對JTA分佈式事務的支持,經過Bitronix實現--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jta-bitronix</artifactId></dependency><!--對javax.mail的支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency><!--對spring-mobile的支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mobile</artifactId></dependency><!--對spring-security模板引擎的支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!--對spring-social-facebook的支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-social-facebook</artifactId></dependency><!--對spring-social-twitter的支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-social-twitter</artifactId></dependency><!--對經常使用測試依賴的支持,包括JUnit,Hamcrest和Mockito還有Spring-test模塊;--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!--對Thymeleaf模板引擎的支持,包括和Spring的集成;--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!--對全棧web開發的支持,包括Tomcat和spring-webmvc;--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--對websocket開發的支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency><!--導入JettyHTTP引擎(做爲Tomcat的代替)--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency><!--導入SpringBoot的默認日誌系統(Logback)--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></dependency><!--導入SpringBoot的默認HTTP引擎(Tomcat)--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></dependency><!--導入UndertowHTTP引擎(做爲tomcat的代替)--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-undertow</artifactId></dependency><!--對GemFire分佈式數據存儲的支持,包括spring-data-gemfire--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-gemfire</artifactId></dependency><!--對java持久化API的支持,包括Spring-data-jpa,spring-orm和Hibernate--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><fork>true</fork><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build></project>原文:https://blog.csdn.net/chen15369337607/article/details/78445773
maven配置詳解:
轉自 http://www.javashuo.com/article/p-ckzkvhfg-gz.htmlspring
2. 配置devtools實現熱部署
devtools的原理mongodb
深層原理是使用了兩個ClassLoader,一個Classloader加載那些不會改變的類(第三方Jar包),另外一個ClassLoader加載會更改的類,稱爲restart ClassLoader,這樣在有代碼更改的時候,原來的restart ClassLoader 被丟棄,從新建立一個restart ClassLoader,因爲須要加載的類相比較少,因此實現了較快的重啓時間。數據庫
使用須要添加如下的配置:apache
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <!-- 此處必須配置,不然 devtools 不會生效--> </configuration> </plugin> </plugins> </build>
說明:瀏覽器
(1) devtools能夠實現頁面熱部署(即頁面修改後會當即生效,這個能夠直接在application.properties文件中配置spring.thymeleaf.cache=false來實現),
實現類文件熱部署(類文件修改後不會當即生效),實現對屬性文件的熱部署。
即devtools會監聽classpath下的文件變更,而且會當即重啓應用(發生在保存時機),注意:由於其採用的虛擬機機制,該項重啓是很快的
(2)配置了true後在修改java文件後也就支持了熱啓動,不過這種方式是屬於項目重啓(速度比較快的項目重啓),會清空session中的值,也就是若是有用戶登錄的話,項目重啓後須要從新登錄。tomcat
默認狀況下,/META-INF/maven,/META-INF/resources,/resources,/static,/templates,/public這些文件夾下的文件修改不會使應用重啓,可是會從新加載(devtools內嵌了一個LiveReload server,當資源發生改變時,瀏覽器刷新)。springboot
devtools的配置
在application.properties中配置spring.devtools.restart.enabled=false,此時restart類加載器還會初始化,但不會監視文件更新。
在SprintApplication.run以前調用System.setProperty(「spring.devtools.restart.enabled」, 「false」);能夠徹底關閉重啓支持,配置內容:
#熱部署生效 spring.devtools.restart.enabled: true #設置重啓的目錄 #spring.devtools.restart.additional-paths: src/main/java #classpath目錄下的WEB-INF文件夾內容修改不重啓 spring.devtools.restart.exclude: WEB-INF/**
IDEA配置
當咱們修改了Java類後,IDEA默認是不自動編譯的,而spring-boot-devtools又是監測classpath下的文件發生變化纔會重啓應用,因此須要設置IDEA的自動編譯:
(1)File-Settings-Compiler-Build Project automatically
(2)ctrl + shift + alt + /,選擇Registry,勾上 Compiler autoMake allow when app running
測試
- 修改類–>保存:應用會重啓
- 修改配置文件–>保存:應用會重啓
- 修改頁面–>保存:應用不會重啓,但會從新加載,頁面會刷新(原理是將spring.thymeleaf.cache設爲false,參考:Spring Boot配置模板引擎)