spring boot 2.0之使用spring boot

简介: spring boot依赖 每一个spring boot的发型版本都包含了所依赖的版本,如果升级spring boot版本,其依赖也会同步更新升级。maven的用户可以通过继承spring-boot-starter-parent。

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 注解可以不用指定包名。如果为根包的话,还可以使用 @S pringBootApplication 标签。其包结构示例如下:
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
目录
相关文章
|
21天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
38 0
|
1月前
|
Java 测试技术 数据库
SpringBoot:@Profile注解和Spring EL
SpringBoot:@Profile注解和Spring EL
|
1月前
|
存储 安全 Java
Spring Boot整合Spring Security--学习笔记
Spring Boot整合Spring Security--学习笔记
52 0
|
1月前
|
消息中间件 Cloud Native Java
【Spring云原生系列】SpringBoot+Spring Cloud Stream:消息驱动架构(MDA)解析,实现异步处理与解耦合
【Spring云原生系列】SpringBoot+Spring Cloud Stream:消息驱动架构(MDA)解析,实现异步处理与解耦合
|
10天前
|
安全 数据安全/隐私保护
Springboot+Spring security +jwt认证+动态授权
Springboot+Spring security +jwt认证+动态授权
|
1月前
|
消息中间件 JSON Java
Spring Boot、Spring Cloud与Spring Cloud Alibaba版本对应关系
Spring Boot、Spring Cloud与Spring Cloud Alibaba版本对应关系
395 0
|
2天前
|
XML Java C++
【Spring系列】Sping VS Sping Boot区别与联系
【4月更文挑战第2天】Spring系列第一课:Spring Boot 能力介绍及简单实践
23 0
【Spring系列】Sping VS Sping Boot区别与联系
|
10天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
12 0
|
1月前
|
缓存 NoSQL Java
spring cache整合redis实现springboot项目中的缓存功能
spring cache整合redis实现springboot项目中的缓存功能
45 1
|
1月前
|
开发框架 前端开发 Java
Spring和Spring Boot:两者的区别与应用场景
Spring和Spring Boot:两者的区别与应用场景
48 1

热门文章

最新文章