Getting started with Spring Boot, configuration, and logging

Keywords: Spring xml SpringBoot encoding

Spring Boot benefits

  • Fast creation of stand-alone Spring projects and integration with mainstream frameworks
  • Using the embedded Servlet container, the application does not need to be a WAR package
  • starters auto dependency and version control
  • A large number of automatic configuration, simplified development, can also modify the default value
  • No XML configuration, no code generation, out of the box
  • Run time application monitoring of quasi production environment
  • Natural integration with cloud computing

YAML file

  • String does not need single or double quotes by default
  • String with double quotes: special characters in the string will not be escaped
  • String with single quotation mark: special characters in the string will be escaped
  • Object representation:
friend:
	lastName: zhanghui
	age: 18

friend: {lastName: zhanghui, age: 18}
  • Array representation:
pet:
	- cat
	- dog
	- pig

pet: [cat,dog,pig]

annotation

// Global profile
@ConfigurationProperties(prefix="haha")

// Specify path profile
@PropertySource(value={"classpath:haha.properties"})

// Import Spring's configuration file
@ImportResource(locations={"classpath:haha.xml"})

Configuration class

@Configuration
public class MyAppConfig{
	// Add the return value of the method to the container, named id
	@Bean
	public Person person() {
		return new Person();
	}
}

Placeholder for profile

  • random number
${random.value}
${random.int}
${random.long}
${random.int(10)}
${random.int[1,10]}
  • Placeholder gets the previously configured value, if not available: Specifies the default value
person.dog.name=${person.name:zhanghui}_dog

Profile

  • Multiple Profile files
    The main configuration file can be named: application-{profile}.properties/yml
    Use the configuration of application.properties by default
  • Activate the specified Profile file
spring.profiles.active=dev
  • YML files support multiple document blocks
server:
	port: 8081
spring:
	profiles:
		active: prod
		
---
server:
	port: 8082
spring:
	profiles: prod
	
  • Activation mode:
// command line
--spring.profiles.active=dev

// configuration file
spring.profiles.active=dev

// jvm parameter
-Dspring.profiles.active=dev

Profile loading (priority from high to low):

SpringBoot will load the main configuration file from all four locations; complementary configuration
Command line -- spring.config.location change default configuration

  • file:./config/
  • file:./
  • classpath:/config/
  • classpath:/

External configuration load order (priority from high to low)

  • Command line arguments
  • application-{profile}.properties or application.yml outside the jar package
  • application-{profile}.properties or application.yml inside the jar package
  • application.properties or application.yml outside the jar package
  • application.properties or application.yml inside the jar package

Auto configuration principle

  1. When Spring Boot is started, the main configuration class is loaded, and the automatic configuration function @ EnableAutoConfiguration is enabled
  2. @EnableAutoConfiguration uses selectImports() of AutoConfigurationImportSelector to add all the values of EnableAutoConfiguration configured in META-INF/spring.factories under the class path to the container
  3. Each xxxautoconfiguration class is a component in the container, and each autoconfiguration class completes an autoconfiguration function
  4. Analyze HttpEncodingAutoConfiguration class:
@Configuration(proxyBeanMethods = false)
// Start the ConfigurationProperties function of the specified class to bind the corresponding value in the configuration file with HttpEncodingProperties
@EnableConfigurationProperties({HttpProperties.class})
// Determine whether the current application is a web application. If so, the configuration class takes effect
@ConditionalOnWebApplication(type = Type.SERVLET)
// Determine whether the current project has this class
// Filter to solve the garbled code in springmvc
@ConditionalOnClass({CharacterEncodingFilter.class})
// Determine whether the configuration exists in the configuration file: spring.http.encoding.enabled
// If not, the value defaults to true
@ConditionalOnProperty(
    prefix = "spring.http.encoding",
    value = {"enabled"},
    matchIfMissing = true
)
public class HttpEncodingAutoConfiguration {

// Get the specified value and bean property from the configuration file to bind
@ConfigurationProperties(prefix = "spring.http")
public class HttpProperties {

Journal

  • The Spring framework uses JCL (Jakarta Commons Logging) by default
  • The spring boot framework uses SLF4j and logback by default
  • Log level (low to high):
    1,trace
    2,debug
    3,info
    4,warn
    5,error
# Set log level
logging.level.com.zh=trace

# Do not specify path to generate springboot under current project.log Journal
# Created under the root path of the current disk, using spring.log As default file
logging.path=/springboot/log

# Specified file
logging.file=G:/springboot.log
  • logback.xml and logback.spring.xml
    logback.xml: directly recognized by the log framework
    logback.spring.xml: the log configuration is parsed by spring boot instead of being directly loaded into the log configuration item. It supports some advanced functions
<springProfile name="staging">
	Specify that a configuration will only take effect in a certain environment
</springProfile>
42 original articles published, praised 2, 10000 visitors+
Private letter follow

Posted by LeslieHart on Sat, 15 Feb 2020 02:05:02 -0800