Spring Boot entry, configuration, log

Spring Boot advantages

  • Quickly create stand-alone projects, and integration with the Spring framework mainstream
  • Use Embedded Servlet container, applications do not need to be labeled WAR package
  • dependent automatic starters and Versioning
  • A large number of auto-configuration, simplifying development, can also modify the default values
  • Without the XML configuration, no code generation, out of the box
  • Application Monitoring run quasi-production environment when
  • Natural integration with the cloud

YAML file

  • String default without adding a single or double quotes
  • String with double quotation marks: there will not escape the string of special characters
  • String in single quotation marks: will escape special characters in a string inside
  • Object, said:
friend:
	lastName: zhanghui
	age: 18

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

pet: [cat,dog,pig]

annotation

// 全局配置文件
@ConfigurationProperties(prefix="haha")

// 指定路径配置文件
@PropertySource(value={"classpath:haha.properties"})

// 导入 Spring 的配置文件
@ImportResource(locations={"classpath:haha.xml"})

Configuration class

@Configuration
public class MyAppConfig{
	// 将方法的返回值添加到容器中,方法名为 id
	@Bean
	public Person person() {
		return new Person();
	}
}

Profiles placeholder

  • random number
${random.value}
${random.int}
${random.long}
${random.int(10)}
${random.int[1,10]}
  • Placeholder value acquired previously configured, may be used if not: specify the default value
person.dog.name=${person.name:zhanghui}_dog

Profile

  • Profile Files Multi
    master configuration file may be named as: application- {profile} .properties / yml
    default configuration application.properties
  • Activate the specified file Profile
spring.profiles.active=dev
  • YML file supports multiple document block
server:
	port: 8081
spring:
	profiles:
		active: prod
		
---
server:
	port: 8082
spring:
	profiles: prod
	
  • Activation:
// 命令行
--spring.profiles.active=dev

// 配置文件
spring.profiles.active=dev

// jvm 参数
-Dspring.profiles.active=dev

Configuration file to load (from highest to lowest priority):

SpringBoot will be loaded from the four positions of all main configuration file; complementary configuration
command to change the default configuration --spring.config.location

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

Loading external configuration order (descending priority)

  • Command line parameters
  • jar outer package or application- {profile} .properties application.yml
  • Internal jar package application- {profile} .properties or application.yml
  • jar or outer package application.properties application.yml
  • Internal jar package application.properties or application.yml

Automatic Configuration Principle

  1. Spring Boot boot time configuration load the main class, opened the automatic configuration @EnableAutoConfiguration
  2. @EnableAutoConfiguration selectImports AutoConfigurationImportSelector use of (), the values ​​of all the META-INF EnableAutoConfiguration classpath / spring.factories arranged inside added to the vessel
  3. Each xxAutoConfiguration class is a container component, each of a complete auto-configuration of an automatic configuration feature class
  4. Analysis HttpEncodingAutoConfiguration categories:
@Configuration(proxyBeanMethods = false)
// 启动指定类的 ConfigurationProperties 功能,将配置文件中对应的值和 HttpEncodingProperties 绑定起来
@EnableConfigurationProperties({HttpProperties.class})
// 判断当前应用是否是web应用,如果是,配置类生效
@ConditionalOnWebApplication(type = Type.SERVLET)
// 判断当前项目有没有这个类
// springmvc 中解决乱码的过滤器
@ConditionalOnClass({CharacterEncodingFilter.class})
// 判断配置文件中是否存在配置:spring.http.encoding.enabled
// 如果不存在,值默认为 true
@ConditionalOnProperty(
    prefix = "spring.http.encoding",
    value = {"enabled"},
    matchIfMissing = true
)
public class HttpEncodingAutoConfiguration {

// 从配置文件中获取指定的值和 bean 的属性进行绑定
@ConfigurationProperties(prefix = "spring.http")
public class HttpProperties {

Journal

  • Spring Framework default JCL (Jakarta Commons Logging)
  • SpringBoot framework used by default SLF4j and logback
  • Log level (from low to high):
    . 1, the trace
    2, Debug
    . 3, info
    . 4, The warn
    . 5, error
# 设置日志级别
logging.level.com.zh=trace

# 不指定路径在当前项目下生成 springboot.log 日志
# 在当前磁盘的根路径下创建,使用 spring.log 作为默认文件
logging.path=/springboot/log

# 指定文件
logging.file=G:/springboot.log
  • logback.xml and logback.spring.xml
    logback.xml: directly by logging framework to identify
    logback.spring.xml: SpringBoot resolved by the log configuration, instead of being loaded directly to the log configuration items, supports some advanced features
<springProfile name="staging">
	指定某段配置只在某个环境下生效
</springProfile>
Published 42 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/sanmao123456_/article/details/104233718