一、Spring boot的使用

1. 使用maven进行构建

用户可以通过继承spring-boot-starter-parent来获取默认的依赖。

  • 默认java1.8编译级别
  • 默认UTF-8编码设置
  • 默认的DependencyManagement,继承与spring-boot-dependencies pom文件,用于管理常用依赖及版本。后续使用添加maven依赖可以省略version信息。
  • repackage 配置
  • 资源文件过滤
  • plugin信息配置(包括exec、shade等)
  • application.properties,application.yml文件及profile环境文件(如application-dev.properties或者application-dev.yml等)过滤。

starter parent 继承,pom依赖:

<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>

后续添加starter依赖,可以使用新的版本号进行覆盖。也可以通过定义相应的属性,来覆盖相应依赖的版本信息。

<properties>
<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>

支持的版本信息可以通过spring-boot-dependencies pom文件来查看。

2. 不通过继承start-parent来使用

有些时候,通过继承starter-parent来使用spring boot并不合适,例如,项目有自定义的需要继承的pom。这种情况下就可以通过使用spring-boot-dependencies pom文件来使用默认依赖(不包括默认插件配置),如下:声明type为pom,scope为import

<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>

</dependency>
</dependencies>
</dependencyManagement>

这种情况下,定义相应的属性配置,将不再能覆盖已有版本信息。如果需要,可以通过使用以下方式,以spring-data-releasetrain为例:

<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.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

3. Spring boot maven插件使用

Spring boot包含一个可以将工程打包成可执行jar的maven插件,可以通过如下方式进行添加:

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

继承starter-parent情况下,只配置此插件即可使用。如果需要更改stater-parent中的默认配置属性,可以通过定义进行覆盖。

二、最佳实践

1. 代码结构:

避免使用默认包

如果创建的类没有声明包信息,则类会默认使用默认包,默认包使用在使用诸如@ComponentScan@EntityScan, 及@SpringBootApplication时可能会引发特殊问题。

官方建议遵循java既有的命名约定规则,使用反转域名的方式命名包。例如,com.example.project.

2. 应用主类位置

通常我们建议将主类放置于根路径下,注解@SpringBootApplication 通常放置于主类上,并且作为么某些扫描的根路径。如JPA配置的Entity扫描等。

@SpringBootApplication注解包含 @EnableAutoConfiguration 和 @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

包含main方法的主类定义如下:

package com.example.myapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }

3. 配置类@Configuration:

Spring boot倾向使用基于java配置类的配置方式,建议使用主类作为主要的配置位置@Configuration。

4. 引入额外的配置类:

不需要将所有的配置放到一个配置类中,可以通过使用@Import注解引入额外的配置类信息。当然@ComponentScan注解会扫描包含@Configuration注解的配置类。

5. 引入xml配置

如果存在不许使用xml配置的情况,则可以通过@ImportResource注解来进行加载。

6. 自动配置@EnableAutoConfiguration

Spring boot基于添加的相应的功能jar进行自动配置。例如,类路径中有HSQLDB jar包的情况下,如果没有主动定义相应的数据源连接bean,则spring boot会自动配置内存数据库。

自动配置需添加相应的@EnableAutoConfiguration或者@SpringBootApplication来启用。通常放置其一于主类即可。

7. 自动配置的覆盖:

自动配置是非侵入性的,可以通过定义相应的自定义配置类进行覆盖,如果需要知道工程目前使用了那些自动配置,可以通过在启动时添加—debug选项,来进行输出。

8. 禁用某些自动配置

如果发现输出的日中包含一些不需要应用的自动配置可以通过在注解@EnableAutoConfiguration上添加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 {

如果相应的配置类不再类路径下,则可以使用excludeName属性进行全路径配置。

另外,也可以通过配置文件spring.autoconfigure.exclude属性进行配置。

9. Spring bean及依赖注入

使用@ComponentScan进行bean的扫描及使用@Autowired进行以来注入。

如果工程结构遵循了以上的建议结构,那么添加@ComponentScan于主类上,就会将所有的诸如@Component@Service@Repository@Controller等注解的类注册为spring bean。

如下示例,使用构造器诸如的@Service注解的服务类:

package com.example.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class DatabaseAccountService implements AccountService { private final RiskAssessor riskAssessor; @Autowired
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
} // ... } 

如果只有一个构造函数,则@Autowired可以省略。

10. @SpringBootApplication:等价以下三个注解默认配置

  • @EnableAutoConfiguration:自动配置
  • @ComponentScan:自动组件扫描
  • @Configuration:配置类注解
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);
} }

包含的注解并不是强制性的,可以自定义的使用相应的注解以使用相应的特性:

package com.example.myapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; @Configuration
@EnableAutoConfiguration
@Import({ MyConfig.class, MyAnotherConfig.class })
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }

如上,不使用自动扫描发现,及主动引入基于java的配置类。

11. 运行你的工程

可以将工程打包为一个可运行的jar,并使用内置的http服务器来运行。

$ 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

使用maven插件运行:

$ mvn spring-boot:run

spring boot 使用及最佳实践的更多相关文章

  1. Spring Boot 16 条最佳实践

    Spring Boot是最流行的用于开发微服务的Java框架.在本文中,我将与你分享自2016年以来我在专业开发中使用Spring Boot所采用的最佳实践.这些内容是基于我的个人经验和一些熟知的Sp ...

  2. Spring Boot使用Liquibase最佳实践

    Liquibase问题 随着项目的发展,一个项目中的代码量会非常庞大,同时数据库表也会错综复杂.如果一个项目使用了Liquibase对数据库结构进行管理,越来越多的问题会浮现出来. ChangeSet ...

  3. 【Spring Cloud】Spring Cloud之整合Spring Cloud Bus以及最佳实践

    一.整合步骤 1)加入Maven坐标 <!-- actuator监控模块 --> <dependency> <groupId>org.springframework ...

  4. Spring Boot消息队列应用实践

    消息队列是大型复杂系统解耦利器.本文根据应用广泛的消息队列RabbitMQ,介绍Spring Boot应用程序中队列中间件的开发和应用. 一.RabbitMQ基础 1.RabbitMQ简介 Rabbi ...

  5. spring boot + mybatis + druid配置实践

    最近开始搭建spring boot工程,将自身实践分享出来,本文将讲述spring boot + mybatis + druid的配置方案. pom.xml需要引入mybatis 启动依赖: < ...

  6. Spring Boot开发MongoDB应用实践

    本文继续上一篇定时任务中提到的邮件服务,简单讲解Spring Boot中如何使用MongoDB进行应用开发. 上文中提到的这个简易邮件系统大致设计思路如下: 1.发送邮件支持同步和异步发送两种 2.邮 ...

  7. Spring Boot WebFlux 快速入门实践

    02:WebFlux 快速入门实践 Spring Boot 2.0 spring.io 官网有句醒目的话是: BUILD ANYTHING WITH SPRING BOOT Spring Boot ( ...

  8. Spring Boot Admin简介及实践

    问题 在若干年前的单体应用时代,我们可以相对轻松地对整个业务项目进行健康检查.指标监控.配置管理等等项目治理.如今随着微服务的发展,我们将大型单体应用按业务模型进行划分,以此形成众多小而自治的微服务, ...

  9. Spring Boot 配置文件 – 在坑中实践

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢!   『 仓廪实而知礼节,衣食足而知荣辱 - 管仲 』   本文提纲 一.自动配置 二.自定义 ...

随机推荐

  1. 使用FASTJSON做反序列化的时间格式处理

    JSONObject.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.mmm"; Productorder tmp1 = JSONObj ...

  2. 关于移动端APP开发-字体样式变大问题

    前两天在写App项目的时候发现一个问题,就是明明css写的样式是14px,刚开始在页面显示时并未出现问题,可是内容一多,字体突然变大了. what?,不明所以,在各大网站上找了好久才知道是浏览器的字体 ...

  3. python处理数据(一)

    CSV数据处理 csv文件格式 逗号分隔符(csv),有时也称为字符分隔值,因为分隔字符也可以不是逗号,其文件以纯文本的形式存储表格数据(数字和文本).纯文本意味着该文件是一个字符序列,不含必须像二进 ...

  4. FreeMaker常用方法

    FreeMaker常用方法 1.表达式转换类 ${expression}计算expression并输出 #{ expression }数字计算#{ expression ;format}安格式输出数字 ...

  5. Java8新特性 -- Lambda 方法引用和构造器引用

    一. 方法引用: 若Lambda体中的内容有方法已经实现了,我们可以使用“方法引用” 要求 方法的参数和返回值类型 和 函数式接口中的参数类型和返回值类型保持一致. 主要有三种语法格式: 对象 :: ...

  6. 20155314 2016-2017-2 《Java程序设计》第1周学习总结

    20155314 2016-2017-2 <Java程序设计>第1周学习总结 学习目标 了解Java基础知识(已完成) 了解JVM.JRE与JDK,并下载.安装.测试JDK(已完成) 了解 ...

  7. Jenkins启动和停止服务

    1.怎么启动Jenkins? step1:进入到Jenkins的war包所在的目录. 如果是win7及以上版本,直接打开Jenkins的war包所在的目录,在地址栏敲cmd,回车. 上述结果和进入cm ...

  8. Octave安装

    转自:https://www.cnblogs.com/freeweb/p/7124589.html Octave是一种解释类的编程语言,并且是GNU项目下的开源软件,与之相对是大家都非常熟悉的matl ...

  9. Google 地图切片URL地址解析

    一.Google地图切片的投影方式及瓦片索引机制 1.地图投影 Google地图采用的是Web墨卡托投影(如下图),为了方便忽略了两极变形较大的地区,把世界地图做成了一个边长等于赤道周长的正方形(赤道 ...

  10. python 内置常用函数

    import os def set(o): return set(o) # =={o} def reverseObject(it): it.reverse() return it def sortOb ...