SpringBoot
原理
自动装配
pom.xml
spring-boot-dependencies 核心依赖在父工程中
引入依赖的时候,不需要指定版本
启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> 启动器是是Springboot的启动场景
springboot会将所有功能场景变成一个个启动器
主程序
SpringBoot配置
@SpringBootApplication :标注这个类是一个springboot应用
@SpringBootConfiguration : Springboot配置
@Configuration :spring配置类
@Component :说明也是一个spring的组件
@EnableAutoConfiguration :自动配置
@AutoConfigurationPackage :自动配置包
@Import({Registrar.class}) :自动配置`包注册`
@Import({AutoConfigurationImportSelector.class}) : 自动配置导入选择
//获取所有配置
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes); 获取候选的配置
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
return configurations;
} 自动配置核心文件
META-INF/spring.factories
自动配置的注解
Conditional注解
关于SpringBoot,你的理解:
- 自动装配
- run方法
SpringBoot配置
yaml
- 简洁的非标记语言
server:
port: 8081
# 普通的key-value key : value
name: Rickduck
# 对象
student:
name: Rickduck
age : 3
# 行内写法
student1: {name: Rickduck,age: 3}
# 数组
pets:
- cat
- dog
- pig
-
pets1: [cat,dog,pig] 特点:
- 可以给实体类赋值
application.yaml:
person:
name: Rickduck
age: 3
happy: false
birth: 1999/9/9
maps: {k1: v1,k2: v2}
lists: [code,music,girl]
dog:
name: yuesheng
age: 3 User实体类
@Component
@Data
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private Boolean happy;
private Date birth;
private Map<String,Object> maps;
private List<String> lists;
private Dog dog;
}
@Component
@Data
@NoArgsConstructor
@AllArgsConstructor
@PrepertySource(value = "url")
// 加载property的内容并自动装配
public class Person {
@Value("${name}")
private String name;
private Integer age;
private Boolean happy;
private Date birth;
private Map<String,Object> maps;
private List<String> lists;
private Dog dog;
} 特点
- 支持松散绑定(不同命名规则可以转换)
- JSR303数据绑定
@Validated //数据绑定
- 复杂类封装类,yml可以封装对象,@Value不支持
多环境配置
配置优先级(高到低)
- file:./config/
- file:./
- classpath:/config/
- classpath:/
方式一:
配置多个以application为前缀的配置文件,在spring.profiles.active中激活
server:
port: 8081
#SpringBoot多环境配置,可以选择激活哪一个配置文件
spring:
profiles:
active: dev 方式二:
同一个yaml文件下通过---分隔多环境 (idea中不推荐使用)
server:
port: 8081
#SpringBoot多环节配置,可以选择激活哪一个配置文件
spring:
profiles:
active: dev
---
server:
port: 8082
spring:
profiles:
dev
---
server:
port: 8083
spring:
profiles:
test yaml 能够配置的内容:
xxxAutoConfiguration : 默认值 xxxProperties 和 配置文件绑定,即可使用自定义配置
debug = true可以查看哪些配置被自动装配了
SpringBoot Web开发
jar: webapp
SpringBoot到底帮我们配置了什么
能不能修改,能修改什么东西
能不能扩展
- xxxAutoConfiguration : 向容器中自动配置组件
- xxxProperties:自动配置类,装配配置文件中自定义的一些内容
要解决的问题
- 导入静态资源
- 首页
- 模板引擎 Thymeleaf
- 装配扩展SpringMVC
- 增删改查
- 拦截器
- 国际化
静态资源
- 在SpringBoot,可以使用以下方式处理静态资源:
- webjars
localhost:8080/webjars/ - public static /** resources
localhost:8080/
- webjars
- 优先级: resources > static(默认)>public
首页
- templates目录下的页面只能通过controller来跳转
- 需要模板引擎的支持! thymleaf
- public/ static/ resources/ index.html --> 首页
templates模板引擎
导入pom依赖
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> 装配扩展SpringMVC
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//视图跳转
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/Rickduck").setViewName("test");
}
} 在Springboot中,有很多xxxConfiguration 帮助进行扩展配置!



京公网安备 11010502036488号