美文网首页
20190212 SpringBoot查漏补缺01

20190212 SpringBoot查漏补缺01

作者: mingmingz | 来源:发表于2019-02-12 11:25 被阅读0次

01. 排除auto-configuration的类

如果你发现auto-configuration classes 配置了一个你不想使用的类,可以在@EnableAutoConfiguration中禁止它:

import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead. Finally, you can also control the list of auto-configuration classes to exclude by using the spring.autoconfigure.exclude property.

如果class不在classpath中, 你可以使用annotation的excludeName 并指定它的全称来代替. 最后, 你也可以通过使用spring.autoconfigure.exclude属性来控制被auto-configuration排除的classes.

02. auto-restart trigger file

当使用spring-boot-devtools时, 你想在特定的时候让程序重启(比如一个文件更新后), 可以使用spring.devtools.restart.trigger-file来配置对应文件.

03. spring-boot-devtools全局设置

你可以在$HOME文件夹下配置一个名为.spring-boot-devtools.properties 的文件作为全局设置,在你的机器上使用devtools的所有Spring Boot应用程序都会应用该文件的属性.例如:

~/.spring-boot-devtools.properties:

spring.devtools.reload.trigger-file=.reloadtrigger

.spring-boot-devtools.properties文件中激活的属性不会影响到profile-specific configuration files

04. Restart Classloader

Spring Boot提供的Restart技术使用了两种类加载器. 没改变的类由 base classloader加载(例如第三方的jar包).你正在开发的类由 restart classloader加载, 当应用程序重新启动时, restart classloader 会被废弃然后创建一个新的. 因为 base classloader 已经可用并且加载好了, 所以这样启动会比 "cold starts"快很多.

05. 日志级别

所有被支持的日志系统都能在Spring Environment中通过logging.level.<logger-name>=<level>进行配置(例如:application.properties), level可以是 TRACE, DEBUG, INFO, WARN, ERROR, FATAL, 或 OFF 中的一个. rrot logger可以使用 logging.level.root来配置.下面的示例展示了application.properties中的一些重要logging设置:

logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener

06. PropertySource order

Spring Boot为了设计出合理的overriding of values, 使用了非常特别的PropertySource顺序. Properties的优先级由高到低如下所示:

  1. 位于$HOME目录的的Devtools global settings properties(~/.spring-boot-devtools.properties when devtools is active).
  2. 位于单元测试上的@TestPropertySource 注解.
  3. 单元测试的properties attribute. 适用于 @SpringBootTesttest annotations for testing a particular slice of your application.
  4. 命令行参数.
  5. 来自于SPRING_APPLICATION_JSON的properties.
  6. ServletConfig 的初始化参数.
  7. ServletContext的初始化参数.
  8. 来自于java:comp/env的 JNDI attributes.
  9. Java System properties(System.getProperties()).
  10. OS环境变量.
  11. 一个只在random.*拥有properties的 RandomValuePropertySource.
  12. 打包好的jar包外的Profile-specific application properties(application-{profile}.properties 和 YAML variants).
  13. 打包好的jar包内的Profile-specific application properties(application-{profile}.properties 和 YAML variants).
  14. 打包好的jar包外的Application properties(application.properties and YAML variants).
  15. 打包好的jar包内的Application properties(application.properties and YAML variants).
  16. @Configuration 类上的@PropertySource 注解.
  17. 默认properties(通过SpringApplication.setDefaultProperties指定).

为了提供一个具体的例子,假设你正在编写一个将要使用name property的 @Component, 如下面的例子:

import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;

@Component
public class MyBean {

    @Value("${name}")
    private String name;

    // ...

}

在你的application classpath(例如,在jar包里面),你可以有application.properties文件来提供一个name的默认值. 当该程序运行在一个新的环境, 可以在jar包外配置一个新的application.properties文件来复写name. 为了一次独立的测试, 你可以指定命令来加载.(例如:java -jar app.jar --name="Spring").

SPRING_APPLICATION_JSON properties可以通过命令行来提供环境变量.例如,在shell中使用如下命令:

$ SPRING_APPLICATION_JSON='{"acme":{"name":"test"}}' java -jar myapp.jar

在上面的例子中, 在Spring Environment 中会有acme.name=test. 你也可以在System property中以spring.application.json的方式提供JSON, 例如:

$ java -Dspring.application.json='{"name":"test"}' -jar myapp.jar.

你也可以通过命令行参数的形式提供JSON, 例如:

$ java -jar myapp.jar --spring.application.json='{"name":"test"}'

你也可以以 JNDI变量的方式提供JSON,例如:java:comp/env/spring.application.json.

07. 访问命令行参数

默认情况下, SpringApplication会将所有命令行参数转换为property并将它们添加到Spring Environment(也就是说以开始的参数,例如--server.port=9000).如前面提到的,命令行参数会优先于任何其他property sources被应用.如果你不想命令行的properties被添加到Environment, 你可以通过SpringApplication.setAddCommandLineProperties(false)来禁用.

08. 应用程序 Propety Files

SpringApplication根据application.properties文件的位置来加载properties,优先级由高到低如下所示:

  1. 当前目录的的/config子目录
  2. 当前目录
  3. classpath的/config
  4. classpath根目录

根据以上优先级, 优先级高的会覆盖掉优先级低的.

09. Relaxed Binding

yaml properties files的属性注入对属性名没那么严格,规则如下例所示:

@ConfigurationProperties(prefix="acme.my-project.person")
public class OwnerProperties {

    private String firstName;

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

}

Table 24.1. relaxed binding

Property Note
acme.my-project.person.first-name Kebab case, 建议用于.properties.yml文件中.
acme.myProject.person.firstName Standard camel case syntax.
acme.my_project.person.first_name Underscore notation,可用于.properties.yml文件中的另一替换方案.
ACME_MYPROJECT_PERSON_FIRSTNAME Upper case format, 推荐用于使用系统环境变量时.

注解中的prefix值必须使用 kebab case.

Table 24.2. relaxed binding rules per property source

Property Source Simple List
Properties Files Camel case, kebab case, or underscore notation 标准list语法,使用[]或逗号分隔符
YAML Files Camel case, kebab case, or underscore notation 标准 YAML list syntax 或逗号分隔符
Environment Variables 使用下划线作为定界符的大写规则. _ 不应该作为属性名的一部分 数值应该被下划线包围,例如:MY_ACME_1_OTHER = my.acme[1].other
System properties Camel case, kebab case, or underscore notation 标准list语法,使用[]或逗号分隔符

如果可以,推荐使用lower-case kebab来存储属性,例如:my.property-name=acme.

当绑定Map类型的属性时,如果key包含非数字,小写字母或-时,你应该使用括号将其括起. 如果key没被[]括起, 那么它们将会被移除. 例如:

acme:
  map:
    "[/key1]": value1
    "[/key2]": value2
    /key3: value3

绑定的Map对象的key会是/key1, /key2 以及 key3 ,第三个key的/被移除了.

10. 标准YAML syntax

使用-来分隔的list:

acme:
  list:
    - name: my name
      description: my description
    - name: another name
      description: another description

map:

acme:
  map:
    key1:
      name: dev name 1
    key2:
      name: dev name 2
      description: dev description 2

相关文章

  • 20190212 SpringBoot查漏补缺01

    01. 排除auto-configuration的类 如果你发现auto-configuration classe...

  • 【Android面试查漏补缺】之事件分发机制详解

    前言 查漏补缺,查漏补缺,你不知道哪里漏了,怎么补缺呢?本文属于【Android面试查漏补缺】系列文章第一篇,持续...

  • 查漏补缺

    如果想让HTML5标签兼容低版本浏览器的话,可以使用 html5shiv js来实现。注意:一定要把它引入到前面。...

  • 查漏补缺

    图文环绕和浮动 最初的CSS只是用来写文章,熟练使用float和clear两个属性来布局: float属性:指定一...

  • 查漏补缺

    1.js字符串转换成数字与数字转换成字符串的实现方法https://www.2cto.com/kf/201612/...

  • 查漏补缺

    浮动原理 1.使用之初:是为了让文字环绕图片,并且不会遮住文字。如果让图片与文字有间距,我们在浮动元素上添加。2....

  • 查漏补缺

    今天数学老师外出学习,我用了一上午的时间给孩子们查漏补缺。 首先,训练朗读:从段落与段落之间的大停顿...

  • 查漏补缺

    1 Python提供了ord()和chr()函数,可以把字母和对应的数字相互转换2 写 u'中' 和 u'\u4e...

  • 查漏补缺

    generator(生成器) 什么是生成器:把一个列表推倒式l = [x for x in range(10)]改...

  • 查漏补缺

    session 判断

网友评论

      本文标题:20190212 SpringBoot查漏补缺01

      本文链接:https://www.haomeiwen.com/subject/obbheqtx.html