一、原理要点
1.1 starters的原理
starters是依赖关系的整理和封装。是一套依赖坐标的整合,可以让导入应用开发的依赖坐标更方便。
starter的命名规范:
官方的starter写法 spring-boot-starter-*
非官方的starter写法thirdpartyproject-spring-boot-starter
starter为什么不需要写版本?
因为添加“parent”标签里的内容后,“spring-boot-starter-parent”会为开发者提供常用jar的版本管理,所以我们不需要指定版本。
1.2 @SpringBootApplication 注解
使用 @SpringBootApplication注解,等价于同时使用
@Configuration 【说明:Spring容器配置类】
@EnableAutoConfiguration
【说明:借助 @Import 的支持,将所有符合自动配置条件的 bean 定义加载到 IoC 容器。】
- 利用EnableAutoConfigurationImportSelector给容器中导入一些组件;
- 可以查看selectImports()方法的内容; List configurations =
getCandidateConfigurations(annotationMetadata, attributes);获取候选的配置 - SpringFactoriesLoader.loadFactoryNames()
@ComponentScan【说明:包扫描】
二、配置要点
2.1application.yml 简要配置
server:
port: 8888
servlet:
# 应用的名称
context-path: /demo
多环境配置
server:
port: 8087
spring:
#指定profiles
profiles:
active: dev
---
#开发环境配置
spring:
profiles: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
---
#测试环境配置
spring:
profiles: test
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://172.148.50.51:3306/mytest?useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
---
#正式环境配置
spring:
profiles: prod
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.100.101:3306/mytest?useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
不同的profiles的配置用三个小横杆来分开。
使用不同配置的三种方式:
1、在配置文件中指定 spring.profiles.active=dev
2、命令行:
java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;
可以直接在测试的时候,配置传入命令行参数
3、虚拟机参数;
-Dspring.profiles.active=dev
三、日志相关
SpringBoot底层使用slf4j+logback的方式进行日志记录
SLF4J是Simple Logging Facade for Java的简写,即Java简单日志门面,用来服务于各种各样的日志框架,比如java.util.logging、logback和log4j。SLF4J允许最终用户在部署时集成自己想要的日志框架。
日志输出格式:
%d表示日期时间,
%thread表示线程名,
%-5level:级别从左显示5个字符宽度
%logger{50} 表示logger名字最长50个字符,否则按照句点分割。
%msg:日志消息,
%n是换行符
–>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n