springboot菜鸟知识点
springboot 菜鸟知识点
- springboot项目创建
-
- 方法1
- 方法2
springboot 可以达成在动配置的原理
* 采用属性文件(如yaml和properties)
*
* 一、yaml语法
*
- yaml语言有严格的书写规范
- 用户可以通过.yaml注释的形式实现代码层面的控制
*
* 二、properties语法
*
- properties文件用于存储非布尔型数据类型的信息
- 它具有简单的结构化数据模型
*
* 三、通过配置文件为Bean中的属性赋值
- 配置文件中指定的键名必须与Bean中的对应字段名一致
- 如果键名不匹配则会导致无法读取的情况发生
- 在不同环境下进行配置文件切换(包括但不限于生产环境和开发环境)。
-
- 采用多份yaml配置文件或properties文件组合。
- 通过单一的yaml文档管理多段文本内容。
-
springboot 配置文件位置和优先级
-
springboot 项目目录说明
-
springboot项目创建
方法1
创建一个普通的maven项目引入springboot依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!--springboot web 相关的启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot 热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--配置文件有代码提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>
spring-boot-configuration-processor
</artifactId>
<optional>true</optional>
</dependency>
<!--springboot 测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--springboot 打包工具,可以打成独立运行的jar-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
创建主类启动springboot:
@SpringBootApplication
public class SpringbootDemo3Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemo3Application.class, args);
}
}
方法2
使用spring initializr 创建springboot 项目。并选择需要的启动器。
springboot 可以实现在动配置的原理
使用@SpringbootApplication标签是一种方法来实现Spring Boot自动配置系统所需的模块。该标签能够扫描启动器所在的包及其子包中的Bean类。可以通过Maven中配置好的启动器类加载所需的自动配置项。这些自动配置项会将相应的模块注入到Spring容器中,以便项目使用。
@SpringbootApplication
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration ----> springboot的配置标签
@EnableAutoConfiguration ----> 实现springboot自动配置的标签(重要)
@ComponentScan(excludeFilters =
{ @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)
})
public @interface SpringBootApplication {
....
}
@EnableAutoConfiguration
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage --->自动扫描包
@Import(AutoConfigurationImportSelector.class) --->选择模块的自动配置类
public @interface EnableAutoConfiguration {
...
}
启动类实例
使用属性文件(yaml、propreties)
application.yaml和application.properties这两个文件中包含了springboot工程的默认配置信息。
1、yaml 语法
dog是一个复杂对象
示例:
# yaml 配置文件是以缩进控制配置文件的结构的,
# 相同的左对齐是同一级别的。再key值得“:”之后必须有一个“ ”。
person:
name: 伟
list: [a,b,b]
maps: {key1: value1, key2: value2}
age: 12
dog:
name: ww
age: 2
2、propreties 语法
person.name=伟
person.age=18
person.list=dog,cat,house
person.maps.key1 = value1
person.maps.key2 = value2
person.dog.name =ww
person.dog.age = 12
person.email= sss@163.com
3、使用配置文件对Bean中的属性进行赋值
该Bean字段通过 @ConfigurationProperties 命令实现外部属性读取功能,默认情况下不带前缀参数设置;如果希望引用外部配置文件,则需借助 @PropertySource注解来指定具体路径定位信息。
除了还可以通过 @Value("${person.name}") 的方式来为属性进行赋值外,这种做法不具备校验功能,并不支持批量注入。
在Spring Boot项目中,模块配置采用了@ConfigurationProperties这一机制来实现。通过特定前缀从配置文件读取信息,并赋予相应的属性参数。
@Validated
@Component
//@PropertySource(value="classpath:person1.properties")
//在使用@ConfigurationProperties 利用yaml配置文件进行属性值注入使,
//属性必须有对应得getter setter方法。
//prefix 是指配置文件中的某一级结构的名称。
//使用@ConfigurationProperties 这种方式可以支持校验功能,
//加上@Validated 和相应的校验注释即可,而@Value 标签不支持校验功能
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
@Email
private String email;
private int age;
private List<String> list;
private Map<String,String> maps;
private Dog dog;
。。。(getter、setter)
}
不同环境中,配置文件切换。(生产环境、开发环境)
1、使用多个yaml,或多个properties文件
实例:
默认情况下,在应用性质文件 application.properties 中采用 spring.profiles.active=pro 机制来指定生效的应用程序性质文件。
该属性的作用在于:采用名为applicationg-${spring.profiles.active}.properties的应用程序性质文件。
yaml性质文件与 properties 文件的操作方式一致。(1) application.properties
#application.properties 配置文件具有最高的优先级,
#默认加载application.properties配置文件中的信息。
#spring.profiles.active属性可以指定加载的配置文件。
#属性值表示的意义为:加载名称为applicationg-${spring.profiles.active}.properties
#的配置文件。如果没有默认的application.properties 也是不会加载其他的配置文件,
#如果想指定可以通过配置虚拟机的启动参数。
spring.profiles.active=pro
2、application-dev.properties
server.port=8081
3、application-pro.properties
server.port = 8082
1、使用一个yaml,配置多个文本块
示例:
文本块以‘——’(共三个‘——’)分隔。第一个文本块被定义为spring.profiles.active的位置名称。其余的文本块则被定义为spring.profiles的位置名称。
spring:
profiles:
active: pro
---
spring:
profiles: dev
server:
port: 8081
---
spring:
profiles: pro
server:
port: 8082
springboot 配置文件位置和优先级
配置文件夹位于项目 directory 下。
项目 overall structure 包括多个部分。
类路径配置 file 夹存于 project directory 下。
类路径 related settings 同样存于 project directory 下。
上级 priority 减少。常规做法是将 configuration file 放置于 class path 下(即 resources 目录)。
configuration file 在打包过程中不会被打包程序包含在内。可以通过启动 parameter 设置 JVM 来实现特定功能。
配置文件夹位于 project directory 下。
项目 overall structure 包括多个部分。
类路径 configuration 夹存于 project directory 下。
类路径 related settings 同样存于 project directory 下。
上级 priority 减少。常规做法是将 configuration file 放置于 class path 下(即 resources 目录)。
configuration file 在打包过程中不会被打包程序包含在内。可以通过启动 parameter 设置 JVM 来实现特定功能。
springboot 项目目录说明
资源静态目录可通过特定URL直接获取。 资源模板目录仅能通过授权方式获取。
