Injection


@Value&ConfigurationProperties

@ConfigurationProperties @Value
Function Batch inject properties from config file Specify one by one (more cumbersome)
SpEL Not supported Supported
Loose binding (loose syntax) Supported Not supported
JSR303 data validation (@Email) Supported Not supported (ineffective if added)
Complex type encapsulation Supported Not supported

@PropertySource:

  • Load specified configuration file

@ImportResource:

  • Load Spring configuration file

    @ImportResource(location = {"xmlFilePath"})
    

    Recommended to use full annotation method:

    // 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 - Creating Multiple Environments


  • Default application.properties
  • Format application-{name}.properties
  • Configuration method:
    • yaml

      • Use document blocks to divide environments

        server:
        	port: 8080
        spring:
        	profiles: 
        	active: {env}
        ---
        server:
        	port: 8081
        spring:
        	profiles: dev
        ---
        server:
        	port: 8082
        spring:
        	profiles: prod
        
    • properties

      • spring.profile.active={env}
      • Command line
        • –spring.profile.active={env}
      • Virtual machine
        • -Dspring.profile.active={env}

Configuration File Loading Order


In the following order (from high to low):

  1. file: ./config/
  2. file: ./
  3. classpath: /config/
  4. classpath: /

High priority files will override low priority files.

To specify a path, use –spring.config.location={path}

For details, see https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config

Configuration


Reference: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties

To view effective auto-configuration classes: debug=true

thymeleaf


HTML File Format

image-20200614000632172

Standard Expressions

  • 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).