spring-boot-starter-parent

Maven的用户可以通过继承spring-boot-starter-parent项目来获得一些合理的默认配置。这个parent提供了以下特性:

  • 默认使用Java 8
  • 使用UTF-8编码
  • 一个引用管理的功能,在dependencies里的部分配置可以不用填写version信息,这些version信息会从spring-boot-dependencies里得到继承。
  • 识别过来资源过滤(Sensible resource filtering.)
  • 识别插件的配置(Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade).)
  • 能够识别application.properties和application.yml类型的文件,同时也能支持profile-specific类型的文件(如: application-foo.properties and application-foo.yml,这个功能可以更好的配置不同生产环境下的配置文件)。
  • maven把默认的占位符${…​}改为了@..@(这点大家还是看下原文自己理解下吧,我个人用的也比较少
    since the default config files accept Spring style placeholders (${…​}) the Maven filtering is changed to use @..@ placeholders (you can override that with a Maven property resource.delimiter).)

starter

启动器包含一些相应的依赖项, 以及自动配置等.

Auto-configuration

Spring Boot 支持基于Java的配置, 尽管可以将 SpringApplication 与 xml 一起使用, 但是还是建议使用 @Configuration.

可以通过 @Import 注解导入其他配置类, 也可以通过 @ImportResource 注解加载XML配置文件.

Spring Boot 自动配置尝试根据您添加的jar依赖项自动配置Spring应用程序. 例如, 如果项目中引入 HSQLDB jar, 并且没有手动配置任何数据库连接的bean, 则Spring Boot会自动配置内存数据库.

您需要将 @EnableAutoConfiguration 或 @SpringBootApplication 其中一个注解添加到您的 @Configuration 类中, 从而选择进入自动配置.

禁用自动配置

import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

如果该类不在classpath中, 你可以使用该注解的excludeName属性, 并指定全限定名来达到相同效果. 最后, 你可以通过 spring.autoconfigure.exclude 属性 exclude 多个自动配置项(一个自动配置项集合)

@ComponentScan

SpringBoot在写启动类的时候如果不使用 @ComponentScan 指明对象扫描范围, 默认指扫描当前启动类所在的包里的对象.

@SpringBootApplication

@Target(value=TYPE)@Retention(value=RUNTIME)@Documented@Inherited@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan(excludeFilters={@ComponentScan.Filter(type=CUSTOM,classes=TypeExcludeFilter.class),})
public @interface SpringBootApplication

使用 @SpringBootApplication 注解相当于使用了下面三个注解.

@EnableAutoConfiguration: 启用 Spring Boot 的自动配置.
@ComponentScan: 对应用程序所在的包启用 @Component 扫描.
@Configuration: 允许在上下文中注册额外的bean或导入其他配置类.

ApplicationRunner or CommandLineRunner 区别

应用服务启动时,加载一些数据和执行一些应用的初始化动作。如:删除临时文件,清除缓存信息,读取配置文件信息,数据库连接等。

1、SpringBoot提供了CommandLineRunner接口。当有该接口多个实现类时,提供了@order注解实现自定义执行顺序,也可以实现Ordered接口来自定义顺序。

注意:数字越小,优先级越高,也就是@Order(1)注解的类会在@Order(2)注解的类之前执行。

import com.example.studySpringBoot.service.MyMethorClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;@Component
@Order(value=1)
public class SpringDataInit implements CommandLineRunner {@Autowiredprivate MyMethorClassService myMethorClassService;@Overridepublic void run(String... strings) throws Exception {int result = myMethorClassService.add(8, 56);System.out.println("----------SpringDataInit1---------"+result);}
}

2、SpringBoot提供的ApplicationRunner接口也可以满足该业务场景。不同点:ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。想要更详细地获取命令行参数,那就使用ApplicationRunner接口

import com.example.studySpringBoot.service.MyMethorClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;@Component
public class SpringDataInit3 implements ApplicationRunner,Ordered {@Autowiredprivate MyMethorClassService myMethorClassService;@Overridepublic void run(ApplicationArguments applicationArguments) throws Exception {int result = myMethorClassService.add(10, 82);System.out.println("----------SpringDataInit3---------"+result);}@Overridepublic int getOrder() {return 3;}
}

外部化配置

Spring Boot允许你外部化你的配置,这样你就可以在不同的环境中使用相同的应用程序代码,你可以使用 properties 文件、YAML文件、环境变量和命令行参数来外部化配置,属性值可以通过使用 @Value 注解直接注入到你的bean中,通过Spring的 Environment 抽象访问,或者通过 @ConfigurationProperties 绑定到结构化对象。

@ConfigurationProperties("spring.datasource.username")

Spring Boot使用一种非常特殊的 PropertySource 命令, 该命令旨在允许对值进行合理的覆盖, 属性按以下顺序考虑:

  • Devtools全局设置属性在你的主目录( ~/.spring-boot-devtools.properties 当devtools处于激活状态时。
  • 测试中的 @TestPropertySource 注解
  • 测试中的 @SpringBootTest#properties 注解属性
  • 命令行参数
  • 来自 SPRING_APPLICATION_JSON(嵌入在环境变量或系统属性中的内联JSON)的属性
  • ServletConfig 初始化参数
  • ServletContext 初始化参数
  • java:comp/env 中的JNDI属性
  • Java系统属性(System.getProperties()
  • 操作系统环境变量
  • 一个只有random.*属性的RandomValuePropertySource
  • 在你的jar包之外的特殊配置文件的应用程序属性(application-{profile}.properties 和YAML 变体)
  • 在jar中打包的特殊配置文件的应用程序属性(application-{profile}.properties 和YAML 变体)
  • 在你的jar包之外的应用程序属性(application.properties 和YAML 变体)
  • 打包在jar中的应用程序属性(application.properties 和YAML 变体)
  • @PropertySource 注解在你的 @Configuration 类上
  • 默认属性(通过设置 SpringApplication.setDefaultProperties 指定)

访问命令行属性

在默认情况下, SpringApplication 会转换任何命令行选项参数 (也就是说,参数从 -- 开始, 像 --server.port=9000) 到一个 property, 并将它们添加到Spring Environment 中, 如前所述, 命令行属性总是优先于其他属性源.

如果不希望将命令行属性添加到 Environment 中, 你可以使用 SpringApplication.setAddCommandLineProperties(false) 禁用它们.

应用程序属性文件

SpringApplication 在以下位置从 application.properties 文件加载属性并将它们添加到Spring Environment :

  • 当前目录子目录的 /config
  • 当前目录
  • 类路径下 /config
  • 类路径的根目录

列表按优先顺序排序(在列表中较高的位置定义的属性覆盖在较低位置定义的属性).

特殊配置文件的属性

我们可能在不同环境下使用不同的配置, 这些配置有可能是在同一个文件中或不同文件中.

1.在相同文件中

##################################### Determime which configuration be used
spring: profiles: active: "dev"# Mysql connection configuration(share)datasource: platform: "mysql"driverClassName: "com.mysql.cj.jdbc.Driver"max-active: 50max-idle: 6min-idle: 2initial-size: 6---
##################################### for dev environment
spring: profiles: "dev"datasource: # mysql connection user(dev)username: "root"# mysql connection password(dev)password: "r9DjsniiG;>7"
---
##################################### for product environment
spring: profiles: "product"datasource: # mysql connection user(product)username: "root"# mysql connection password(product)password: "root"
---
##################################### for test environment
spring: profiles: "test"datasource: # mysql connection user(test)username: "root"# mysql connection password(test)password: "root"

这样在配置完相同属性的时, 还可以对不同的环境进行不同的配置.

2.多个配置文件.

我们可以把特定环境的配置, 放入多个配置文件中, 但是要按照 application-{profile}.properties 格式. 如下图.

spring.profiles.active 属性进行设置.

我们也可以把配置文件放在 jar 外面, 使用 spring.config.location 属性进行设置.

java -jar beetltest-0.0.1-SNAPSHOT.jar -spring.config.location=classpath:/application.properties

最渣的 Spring Boot 文章相关推荐

  1. Spring Boot 的 10 个核心模块

    学习 Spring Boot 必须得了解它的核心模块,和 Spring 框架一样,Spring Boot 也是一个庞大的项目,也是由许多核心子模块组成的. 你所需具备的基础 告诉你,Spring Bo ...

  2. Spring Boot 注册 Servlet 的三种方法,真是太有用了!

    2019独角兽企业重金招聘Python工程师标准>>> 本文栈长教你如何在 Spring Boot 注册 Servlet.Filter.Listener. 你所需具备的基础 什么是 ...

  3. jar打包 剔除第三方依赖以及它的依赖_为什么Spring Boot的 jar 可以直接运行?

    点击上方 Java后端,选择 设为星标 优质文章,及时送达 作者:fangjian0423来自:https://urlify.cn/uQvInaSpringBoot提供了一个插件spring-boot ...

  4. Spring Boot+MyBatis Plus+JWT 问卷系统!开源!

    你好呀,我是 Guide!这里是 JavaGuide 的「优质开源项目推荐」第 8 期,每一期我都会精选 5 个高质量的 Java 开源项目. 时间过的真快,不知不觉「优质开源项目推荐」系列已经持续半 ...

  5. 推荐一个基于 Spring Boot+MyBatis Plus+JWT 的问卷系统!

    你好呀,我是 Guide!这里是 JavaGuide 的「优质开源项目推荐」第 8 期,每一期我都会精选 5 个高质量的 Java 开源项目. 时间过的真快,不知不觉「优质开源项目推荐」系列已经持续半 ...

  6. Spring Boot学习笔记 [完结]

    Spring Boot 文章目录 Spring Boot 1.微服务 1.1 什么是微服务? 1.2 单体应用架构 1.3 微服务架构 2. 第一个SpringBoot程序 2.1.原理 2.2 原理 ...

  7. Spring Boot + redis解决商品秒杀库存超卖,看这篇文章就够了

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:涛哥谈篮球 来源:toutiao.com/i68366119 ...

  8. 构建Spring Boot程序有用的文章

    构建Spring Boot程序有用的文章: http://www.jb51.net/article/111546.htm 转载于:https://www.cnblogs.com/xiandedante ...

  9. Spring Boot 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统...

    一.概述 经过HelloWorld示例(Spring Boot 快速入门(上)HelloWorld示例)( Spring Boot  快速入门 详解 HelloWorld示例详解)两篇的学习和练习,相 ...

最新文章

  1. 编程沉思-做一款小巧而好用的截图软件
  2. linux shell 设置 标准 错误流 输出流 不显示
  3. 总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告
  4. C# 移除数组中重复项
  5. 内核同步机制-读写信号量(rw_semaphore)
  6. Linux学习笔记016---CentOS7虚拟机设置使用静态IP上网_配置集群的时候可以用
  7. 八、Java的可变参数例子
  8. Java基础知识拾遗--IO篇
  9. tomcat启动时出现Error starting static Resources 错误
  10. 小芋头君的知乎 Live 直播-前端开发者成长之路
  11. Codeigniter3学习笔记三(创建类库及使用原生类库)
  12. 【测试人生】安卓FPS测试详解
  13. 数字孪生工厂解决方案,3DGIS+视频融合+时空位置智能(LI)技术
  14. 自学java后都是怎么找的工作?
  15. 使用 Laragon 在 Windows 中快速搭建 Laravel 本地开发环境 (转)
  16. UE学习笔记(一)UC++基础类
  17. DHCP:(5)华为防火墙USG上部署DHCP服务以及DHCP中继
  18. oracle简单查询语句
  19. 【python学习】——为exe软件创建快捷方式;实现软件自启动
  20. 1.3常规信息系统集成技术

热门文章

  1. boost::geometry::detail::copy_segments的用法测试程序
  2. boost::fusion::convert用法的测试程序
  3. boost::callable_traits是否为is_volatile_member的测试程序
  4. Boost:bimap双图operator bracket的测试程序
  5. ITK:多路输出相同类型的
  6. DCMTK:测试ConcatenationCreator类
  7. VTK:可视化算法之VelocityProfile
  8. OpenCV SURF FLANN匹配单应性的实例(附完整代码)
  9. OpenGL设置透视投影并渲染旋转的立方体
  10. C语言煎饼排序Pancake sort算法(附完整源码)