SpringBoot2核心技术

igxiaoshan Lv5

核心功能

配置文件

文件类型

properties

yaml

基本用法

  • key: value kv之间有空格
  • 大小写敏感
  • 缩进不允许使用tab, 只允许空格
  • 缩进的空格数不重要, 只要相同层级的元素左对齐即可
  • '#'表示注释
  • 字符串无需加引号, 如果要加,一些特殊字符会被转义

    • 如果字符串是一串数字,该数字以’零’开头,必须加上’引号’,不然会被解析成八进制

    • 转义如同: System.out.println("name\nage");其中\n就会被转义成换行

propertiesyml加载顺序

数据类型

  • date,boolean,string,number,null
  • map,hash,set,object

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Data
@Configuration
@ConfigurationProperties(prefix = "igsshan")
public class Person {

private String name;
private Integer age;
private Boolean enabled;
private Date birth;
private Pet pet;
private String[] interests;
private List<String> animal;
private Map<String, Object> score;
private Set<Double> salarys;
private Map<String, List<Pet>> allPets;
}

@Data
public class Pet {
private String name;
private Double weight;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
igsshan:
name: 张三
age: 18
enabled: false
birth: 1998/07/19 12:12:12
pet:
name: tomcat
weight: 23.8
interests: [羽毛球,乒乓球]
animal:
- tom
- jerry
score:
english:
first: 30
second: 40
third: 50
math: [120,150,200]
chinese: {first: 128,second: 369}
salarys: [3999,4999.98,5999.99]
allPets:
sick:
- {name: tom}
- {name: jerry,weight: 33}
health: [{name: mario,weight: 44}]

Web开发

官方文献

Spring MVC Auto-configuration

Spring Boot provides auto-configuration for Spring MVC that works well with most applications. It replaces the need for @EnableWebMvc and the two cannot be used together. In addition to Spring MVC’s defaults, the auto-configuration provides the following features:

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
    • 包含内容协商视图解析器BeanName视图解析器
  • Support for serving static resources, including support for WebJars (covered later in this document ).
    • 支持提供静态资源,包含对 WebJars的支持
  • Automatic registration of Converter, GenericConverter, and Formatter beans.
    • 自动注册Converter,GenericConverterFormatter
  • Support for HttpMessageConverters (covered later in this document ).
    • 支持HttpMessageConverters
  • Automatic registration of MessageCodesResolver (covered later in this document ).
    • 自动注册MessageCodesResvoler
  • Static index.html support.
    • 支持静态index.html页面
  • Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document ).
    • 自动使用ConfigurableWebBindingInitializer

If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.

  • 不使用EnableWebMvc注解. 使用Configuration+WebConfigurer自定义规则

If you want to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, and still keep the Spring Boot MVC customizations, you can declare a bean of type WebMvcRegistrations and use it to provide custom instances of those components. The custom instances will be subject to further initialization and configuration by Spring MVC. To participate in, and if desired, override that subsequent processing, a WebMvcConfigurer should be used.

  • 声明WebMvcRegistrations改变默认底层组件

If you do not want to use the auto-configuration and want to take complete control of Spring MVC, add your own @Configuration annotated with @EnableWebMvc. Alternatively, add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.

  • 使用@EnableMvc+@Configuration+DelegatingWebMvcConfiguration全面接管 SpringMvc

静态资源访问