spring boot 2.0之使用spring boot
每一個spring boot的發型版本都包含了所依賴的版本,如果升級spring boot版本,其依賴也會同步更新升級。maven的用戶可以通過繼承spring-boot-starter-parent。其包含了一些合理的值的設置:
1. 默認設置的編譯器為JDK 1.8
2. 源文件默認UTF-8編碼
3. 從spring-boot-dependencies pom中繼承的 Dependency Management 版本信息,在使用的時候,可以忽略其具體的版本信息。
4. 合理的資源過濾(filter)
5. 合理的插件配置
6. 對於application.properties的合理的過濾
7. 默認的配置文件接收spring的${}表達式,maven的資源過濾符被修改為@..@,可以使用maven屬性resource.delimiter進行設置更改。
實現starter-parent的方式有如下兩種:
1. 繼承starter-parent的配置文件如下:
<!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.BUILD-SNAPSHOT</version> </parent>
也可以在自己的pom文件中修改指定的依賴版本信息:例如要更新 Spring Data 為另一個版本。具體代碼如下
<properties> <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version> </properties>
2. 在dependencyManagement
中加入依賴管理:如下代碼所示:
<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> <scope>import</scope> <type>pom</type> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.0.BUILD-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
(備註:type表名導入的類型為POM,scope=import:完全導入POM的依賴)
通過使用 Maven plugin 插件將其打包為一個可執行的文件:代碼如下:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
starters
starter是應用中一系列依賴的描述,可以不用太多依賴就能獲取相應的依賴包。官方給出的starter的命名方式為:spring-boot-starter-*,非官方定義的starter不應當以spring-boot-starter開始,而應當以項目工程為開始例如:thirdpartyproject-spring-boot-starter
starters的列表如下:
名稱 | 描述 |
spring-boot-starter | 核心starter,包含了自動配置支持,logging以及yaml |
spring-boot-starter-activemq | 使用apache ACTIVEMQ的JMS處理STARTER |
spring-boot-starter-amqp | 使用spring AMQP以及rabbit MQ的starter |
spring-boot-starter-aop | 使用spring AOP以及APJECT J的starter |
spring-boot-starter-artemis | 使用apache Artemis作為JMS的starter |
spring-boot-starter-batch | 使用spring batch的starter |
spring-boot-starter-cache | 支持spring framwork cache的starter |
spring-boot-starter-cloud-connectors | spring cloud connectors的連接雲平臺couchbase服務的連接器,像like Cloud Foundry 與Heroku |
spring-boot-starter-data-cassandra | 使用cassandra分布式數據庫以及Spring Data Cassandra的starter |
spring-boot-starter-data-cassandra-reactive | 使用cassandra分布式數據庫以及Spring Data Cassandra 反應的starter |
spring-boot-starter-data-couchbase | 使用面向對象的數據庫以及Spring Data Couchbase的數據庫 |
spring-boot-starter-data-couchbase-reactive | 與前面類似 |
spring-boot-starter-data-elasticsearch | 使用彈性搜索以及分析引擎spring DAta Elasticsearch |
spring-boot-starter-data-jpa | 使用hibernate的 spring JPA starter |
spring-boot-starter-data-ldap | Spring Data LDAP 的starter |
spring-boot-starter-data-mongodb | 使用mongodb的starter |
spring-boot-starter-data-mongodb-reactive | 與前面類似 |
spring-boot-starter-data-neo4j | 使用Neo4j graph database 與 Spring Data Neo4j的starter |
spring-boot-starter-data-redis | 使用key,value存儲在Data Redis的數據庫 |
spring-boot-starter-data-redis-reactive | 與前面類似 |
spring-boot-starter-data-rest | 通過使用spring data rest暴露在spring data repostory。 |
spring-boot-starter-data-solr | 通過使用apache solr平臺的starter |
spring-boot-starter-freemarker | 通過使用freemarker的MVC web 應用的starter |
spring-boot-starter-groovy-templates | 使用groovy template創建的MVC web應用 |
spring-boot-starter-hateoas | 使用Spring MVC 與 Spring HATEOAS構建的超媒體的starter |
spring-boot-starter-integration | spring 集成starter |
spring-boot-starter-jdbc | 使用tomcat的jdbc的連接池鏈接JDBC的starter |
spring-boot-starter-jersey | 使用JAX-RS 與 Jersey構建的web應用程序,也可以使用spring-boot-starter-web |
spring-boot-starter-jooq | 使用jooq鏈接數據庫的starter,其可以替換為:spring-boot-starter-jdbc或者是spring-boot-starter-data-jpa |
spring-boot-starter-json | 讀或者寫json的starter |
spring-boot-starter-jta-atomikos | 使用atomikos的JTA事務starter |
spring-boot-starter-jta-bitronix | 使用bitronix的事務starter |
spring-boot-starter-jta-narayana | 使用narayana的JTA事務starter |
spring-boot-starter-mail | 使用jmail與spring framwork 支持的方式發送郵件 |
spring-boot-starter-mustache | shiyong mustache創建對應的web應用 |
spring-boot-starter-quartz | 定時調用的starter |
spring-boot-starter-security | 安全的starter |
spring-boot-starter-test | 測試的starter |
spring-boot-starter-thymeleaf | 使用thymeleaf構建MVC web應用的starter |
spring-boot-starter-validatior | 通過使用hibernate驗證器驗證的starter |
spring-boot-starter-web | 使用tomcat作為默認容器構建包括的restful,使用MVC的web應用的starter |
spring-boot-starter-web-services | 使用web services的starter |
spring-boot-starter-webflux | 使用 Spring Framework’s Reactive Web support支持的方式構建WebFlux 應用 |
spring-boot-starter-websocket | 通過使用Spring Framework’s WebSocket support構建dewebsocker的starter |
通過以下starter可以監控和管理應用:
spring-boot-starter-actuator | 提供生產的一些特性來進行監控和管理的starter |
結構化代碼
spring boot不需要指定的具體的代碼結構,但是這兒有一些常用的一些架構:
1. 使用默認包:使用默認包的話,會導致@ComponentScan
, @EntityScan
, 或者 @SpringBootApplication
註冊掃描產生一定的問題。因此常推薦的包結構為:
com.example.project
2. 主要main應用程序的位置:推薦放到root package的目錄上。@EnableAutoConfiguration註解將放在main類上,用於搜索包裏面的某些指定項(例如:JPA的包,將會搜索@entity標簽)。使用@ComponentScan註解可以不用指定包名。如果為根包的話,還可以使用@SpringBootApplication標簽。其包結構示例如下:
com +- example +- myapplication +- Application.java | +- customer | +- Customer.java | +- CustomerController.java | +- CustomerService.java | +- CustomerRepository.java | +- order +- Order.java +- OrderController.java +- OrderService.java +- OrderRepository.java
類Application.java 使用@Configuration聲明為啟動類:其代碼如下:
package com.example.myapplication; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
1. 盡管基於XML的spring配置是可用,這裏還是推薦基於java的配置。
2. 主要的資源都要用@Configuration進行配置。
3. 使用@Import可以導入一個主要的資源類。
4. 對於依賴的jar的配置:可以通過@EnableAutoConfiguration
or @SpringBootApplication
進行配置加載對應的配置數據。
5. 自動配置沒有侵入性,在任何時候,你都可以自定義你的配置去替換掉自動配置部分。
6. 如果要找到哪些自動配置被應用,可以使用debug的方式運行程序,可以通過日誌在控制臺顯示出對應的報告信息。
7. 如果不想使用自動配置,可以使用exclude進行排除(也可以通過spring.autoconfigure.exclude處理)代碼如下:
import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.jdbc.*; import org.springframework.context.annotation.*; @Configuration @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) public class MyConfiguration { }
8. 當使用了@ComponentScan,所有的( (@Component
, @Service
, @Repository
, @Controller
etc.)都會被註入spring bean中。通過使用@Autowired
可以自動裝配對象。
9.spring boot 提供了 @SpringBootApplication
來對需要通知引入(@Configuration
, @EnableAutoConfiguration
, 與 @ComponentScan
)進行了簡化:代碼如下:
package com.example.myapplication; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
運行方式
01. 在IDE中運行(eclipse,intel idea)
02. 打包運行
java -jar target/myapplication-0.0.1-SNAPSHOT.jar
也可以進行遠程調試:代碼如下
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar target/myapplication-0.0.1-SNAPSHOT.jar
03. 通過maven運行:
mvn spring-boot:run
可以通過設置環境變量來進行設置處理(export MAVEN_OPTS=-Xmx1024m)
開發工具
spring-boot-devtools
1. spring boot 提供了一系列的工具來方便其進行開發,spring-boot-devtools提供了開發的便利,可以通過以下引入依賴模塊:代碼如下
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies>
2. 在生產環境中,常常使用緩存來提升性能,但在開發環境下,該配置是不可用的,因此spring-boot-devtools默認會設置其不可用。可以通過在application.properties中配置對應的參數:Thymeleaf 提供了spring.thymeleaf.cache設置,spring-boot-devtools能夠自動監控。
3. 應用程序可以通過spring-boot-devtools來監控classpath上的變化來自動重啟,這在IDE中相當有用的。靜態資源的改變不會被重啟(eclipse:保存修改的文件會自動生效。intel idea:需要運行Build -> Make Project)。
4. 每次重啟的時候:都會打印一些日誌報告信息:可以通過spring.devtools.restart.log-condition-evaluation-delta=false不讓其打印。
5. 可以通過設置具體的哪些資源變化不會重啟,其配置如下:spring.devtools.restart.exclude=static/**,public/**
6. 可以通過添加spring.devtools.restart.additional-paths路徑來動態增加。
7. 可以通過在application.propterties中或者在啟動類中加入spring.devtools.restart.enabled 屬性來控制器不重啟。
8. 可以通過使用trigger文件在指定只有該文件修改的時候,才會觸發重啟:其配置為spring.devtools.restart.trigger-file
9. 可以在META-INF/spring-devtools.propertie中加入如下配置自動化其配置:代碼如下:
restart.exclude.companycommonlibs=/mycorp-common-[\\w-]+\.jar restart.include.projectcommon=/mycorp-myproj-[\\w-]+\.jar
10. 可以在根路徑下加入spring-boot-devtools.properties文件,自定義其裏面的配置。
11. 如果遠程調試的話,需要在maven的plugin中指定如下
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludeDevtools>false</excludeDevtools> </configuration> </plugin> </plugins> </build>
設置其對應的spring.devtools.remote.secret屬性。
文檔參考地址:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference
spring boot 2.0之使用spring boot