Spring Boot 學習02-----Dependency以及配置等
繼上一篇我們搭建了一個非常簡單的Spring Boot的demo。接下來,我們將繼續(xù)學習Spring Boot的相關知識。閑話少敘。
本Spring Boot的版本是2.0.0.RELEASE
:
Dependency Management
繼承 spring-boot-starter-parent
默認是:
Java 1.8
UTF-8
Resource filtering。exec plugin、surefire、Git commit ID、shade。
如果不想使用Spring Boot中的默認版本,可以再<properties>
覆蓋相應的版本,如,想使用不同版本的Spring Data,具體如下:
<properties>
<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>
想使用不同版本的JDK
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 使用 java 1.8 -->
<java.version>1.8</java.version>
</properties>
pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
不繼承spring-boot-starter-parent
這種情況下,仍然可以使用dependency management
,但不能使用plugin management
方式如下:
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
注意,socpe 是import。而且,這種情況下,不再允許<properties>
覆蓋相應的版本。如果要使用其他版本,需要在上面的前面添加一個完整的dependency
。如下:
<dependencyManagement>
<dependencies>
<!-- Override Spring Data release train provided by Spring Boot -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Starters
可以創(chuàng)建自己的Starters,但名字格式不能是spring-boot-starter-*
,而是*—spring-boot-starter
。類似于Maven的插件。各種starter
自動配置
@Import和@ComponentScan 類似;
@EnableAutoConfiguration 和@SpringBootAppliction 類似;注意,只能使用一次,建議用在primary @Configuration class上
注意,自動配置永遠都是第二位,一旦你配置了自己的東西,那自動配置的就會被覆蓋。
禁用特定的自動配置:
import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;
@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}
如果class不在classpath 中,可以使用excludeName
,然后使用全路徑即可。
Spring Beans和依賴注入
@SpringBootApplication等同于默認屬性的
@Configuration, @EnableAutoConfiguration and @ComponentScan。
– 注意,@ComponentScan 不能憑空使用。
@Service 用在業(yè)務層
@Controller 用在Controller層(控制層)
@Repository 用在數(shù)據(jù)層
@Autowired 用于注入bean
運行fat jar(executable jar)
java -jar target/xxx.jar
啟動遠程調試支持
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
-jar target/myapplication-0.0.1-SNAPSHOT.jar
使用Maven Plugin
mvn spring-boot:run
設置系統(tǒng)的環(huán)境值
export MAVEN_OPTS=-Xmx1024m
熱加載
因為Spring Boot應用都是簡單的Java應用,所以JVM Hot-swapping 可以直接使用,但是,JVM Hot-swapping 對于能夠替換的字節(jié)碼有些限制,所以建議使用JRebel 或者Spring Loaded
spring-boot-devtools 模塊同樣包含了快速重啟應用的支持,但是只能在debug期間修改方法體。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
注意,生產(chǎn)環(huán)境下(java -jar 或者通過特定的類加載器啟動),這些開發(fā)工具則自動被禁止。
上面的<optional>true</optional>
可以有效阻止用于其他模塊。如果你想確保生產(chǎn)Build中不包含devtools,可以使用excludeDevtools build property。
Property defaults
一些Spring Boot 支持的庫使用了caches 增強性能。例如, template engines。但是cache,在開發(fā)過程中可能會是一個阻礙,例如你無法立即更新一個模塊(thymeleaf的)。cache 設置通常在application.properties中。但是,比起手動設置這些,spring-boot-devtools
模塊會自動應用這些開發(fā)期的設置。
自動重啟
使用spring-boot-devtools
模塊的應用,當classpath中的文件有改變時,會自動重啟!—就是說,默認會監(jiān)視classpath入口。
靜態(tài)資源和視圖模板不需要重啟!
注意,不同的IDE有不同的表現(xiàn),例如Eclipse中只要改變了文件并保存,那么就會導致classpath中的內容改變,而IDEA則需要Build -> Make Project。
可以通過 build plugin 啟動應用,只要開啟了forking支持,因為Devtools需要一個隔離的classloader才能運行正常。Maven下要這樣開啟:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
注意:在使用LiveReload(熱加載)時,自動重啟仍然可以正常工作。如果你使用了JRebel,自動重啟會被禁用以支持動態(tài)類加載。此時,devtools其他方面仍然可以使用。
注意:DevTools在重啟過程中依賴應用上下文的shutdown hook來關掉它,所以如果禁用了shutdown hook,他就無法正常工作了。
SpringApplication.setRegisterShutdownHook(false)
Spring Boot使用的重啟技術,實際上是使用了兩個類加載器:不變的base類加載器,可變的restart 類加載器。前者加載第三方jar之類的。后者加載項目代碼。重啟的時候,只是丟棄可變的restart 類加載器,然后重新創(chuàng)建一個。所以速度比較快。
如果你還是覺得不夠快,你可以考慮使用JRebel
一些特定的資源改變時沒有必要引起重啟。有一些不會引起重啟,但是會重加載。如果你想自定義的設置一下,可以使用spring.devtools.restart.exclude
屬性,例如,要排除/static
以及/public
,你可以設置如下屬性。
spring.devtools.restart.exclude=static/**,public/**
如果你想在修改classpath之外的文件時也讓應用重啟,可以使用spring.devtools.restart.additional-paths
屬性。還可以配合上面提到的spring.devtools.restart.exclude
屬性,來控制是重啟還是重加載。
如果不想使用自動重啟功能,可以使用spring.devtools.restart.enabled
屬性。多數(shù)情況下,可以再application.properties
中設置,這樣仍然會創(chuàng)建一個restart類加載器。但不在監(jiān)視改變。
如果想完全禁止自動重啟,需要在調用SpringApplication.run(..) 之前設置一個System屬性。如下:
public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(MyApp.class, args);
}
如果你的IDE可以隨改隨編譯,你可能會傾向于只在特定時刻引發(fā)重啟,這時,你可以使用trigger file
,就是一個特定的文件。只有修改了這個文件才會觸發(fā)重啟,使用spring.devtools.restart.trigger-file
屬性即可。
自定義restart類加載器。
如果有一個多模塊項目,只有部分導入到你的IDE中,你可能需要自定義一下。首先創(chuàng)建一個文件:META-INF/spring-devtools.properties。該文件中,可以有以前綴 restart.exclude. 和 restart.include. 開頭的屬性。前者會被放入base類加載器,后者則被放入restart類加載器。
該屬性的value,是正則表達式。例如:
restart.include.companycommonlibs=/mycorp-common-[\\w-]+\.jar
restart.include.projectcommon=/mycorp-myproj-[\\w-]+\.jar
已知限制
自動重啟,在使用 ObjectInputStream
反序列化時,會出現(xiàn)問題。如果你需要反序列化數(shù)據(jù)時,你或許需要使用Spring的ConfigurableObjectStream
配合Thread.currentThread().getContextClassLoader()
使用。
可惜,一些第三方的jar沒有考慮到這個問題。無解。
熱加載LiveReload
spring-boot-devtools 模塊內置了一個liveReload Server,可以保證在改變資源時瀏覽的刷新。LiveReload 的瀏覽器擴展,免費支持Chrome,F(xiàn)irefox以及Safari。
如果你想禁用 LiveReload server,你可以設置spring.devtools.livereload.enabled
屬性為false。
注意:只能運行一個LiveReload Server。如果同時開啟多個項目,那只有第一個。
引用
https://www.cnblogs.com/larryzeal/p/5799195.html#c4-3
https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#getting-started-first-application-executable-jar
作者:碼農(nóng)飛哥
微信公眾號:碼農(nóng)飛哥