spring-boot-note
注入
@Value&ConfigurationProperties
| @ConfigurationProperties | @Value | |
|---|---|---|
| 功能 | 批量注入配置文件中的属性 | 一个个指定(较麻烦) |
| SpEL | 不支持 | 支持 |
| 松散绑定(松散语法) | 支持 | 不支持 |
| JSR303数据校验(@Email) | 支持 | 不支持(加了之后无效) |
| 复杂类型封装 | 支持 | 不支持 |
@PropertySource:
- 加载指定的配置文件
@ImportResource:
-
加载Spring配置文件
@ImportResource(location = {"xmlFilePath"})推荐使用全注解的方式:
// Bean id is the name of the method // the return value is the object in IOC container. @Configuration public class ConfigurationClass{ @Bean public BeanMethod beanMethod(){ ... return new Bean; } ... }
Profile创建多个环境
-
默认application.properties
-
格式application-{name}.properties
-
配置方式:
-
yaml
-
用文档块来分环境
server: port: 8080 spring: profiles: active: {env} --- server: port: 8081 spring: profiles: dev --- server: port: 8082 spring: profiles: prod
-
-
properties
- spring.profile.active={env}
- 命令行
- –spring.profile.active={env}
- 虚拟机
- -Dspring.profile.active={env}
-
配置文件加载顺序
按以下顺序(由高到低):
- file: ./config/
- file: ./
- classpath: /config/
- classpath: /
高优先级文件会覆盖低优先级的文件。
若要指定路径,请用 –spring.config.location={path}
具体请看 https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config
配置
参照:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties
查看生效的自动配置类:debug=true
thymeleaf
HTML 文件格式

标准表达式
- Simple expressions:
- Variable Expressions: ${…}
- Selection Variable Expressions: *{…}
- Message Expressions: #{…}
- Link URL Expressions: @{…}
- Fragment Expressions: ~{…}
- Literals
- Text literals: ‘one text’ , ‘Another one!’ ,…
- Number literals: 0 , 34 , 3.0 , 12.3 ,…
- Boolean literals: true , false
- Null literal: null
- Literal tokens: one , sometext , main ,…
- Text operations:
- String concatenation: +
- Literal substitutions: |The name is ${name}|
- Arithmetic operations:
- Binary operators: + , - , * , / , %
- Minus sign (unary operator): -
- Boolean operations:
- Binary operators: and , or
- Boolean negation (unary operator): ! , not
- Comparisons and equality:
- Comparators: > , < , >= , <= ( gt , lt , ge , le )
- Equality operators: == , != ( eq , ne )
- Conditional operators:
- If-then: (if) ? (then)
- If-then-else: (if) ? (then) : (else)
- Default: (value) ?: (defaultvalue)
- Special tokens:
- No-Operation: _
Message Expressions: #{...}
#ctx : the context object.
#vars: the context variables.
#locale : the context locale.
#request : (only in Web Contexts) the HttpServletRequest object.
#response : (only in Web Contexts) the HttpServletResponse object.
#session : (only in Web Contexts) the HttpSession object.
#servletContext : (only in Web Contexts) the ServletContext object
#execInfo : information about the template being processed.
#messages : methods for obtaining externalized messages inside variables expressions, in the same way as they
would be obtained using #{…} syntax.
#uris : methods for escaping parts of URLs/URIs
Page 20 of 106#conversions : methods for executing the configured conversion service (if any).
#dates : methods for java.util.Date objects: formatting, component extraction, etc.
#calendars : analogous to #dates , but for java.util.Calendar objects.
#numbers : methods for formatting numeric objects.
#strings : methods for String objects: contains, startsWith, prepending/appending, etc.
#objects : methods for objects in general.
#bools : methods for boolean evaluation.
#arrays : methods for arrays.
#lists : methods for lists.
#sets : methods for sets.
#maps : methods for maps.
#aggregates : methods for creating aggregates on arrays or collections.
#ids : methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).