SpringBoot核心配置和注解
1.全局配置文件
两种类型
第一种application.properties
#普通属性值的配置
server.port=8081
server.servlet.context-path=/chapter02
#对象类型
person.id = 1
person.name = zhangsan
person.hobby = play,read,sleep
person.family = father,mother
persom.map.k1 = v1
person.map.k2 = v2
person.pet.type = dog
persom.pet.name = kity
第二种application.yaml
#当value值为普通数据类型的配置
server:
port: 8082
servlet:
context-path: /hello
#当value值为数组或者单列集合
#方式一
hobby:
- play
- read
- sleep
#方式二
hobby: [play,read,sleep]
#当value值为map时
#方式一
map:
k1: v1
k2: v2
k3: v3
k4: v4
#方式二
map: {k1: v1,k2: v2}
#对实体类对象person进行配置
person:
id: 2
name: lisi
hobby: [play,read,sleep]
family: [father,mother]
map: {k1: v1,k2: v2}
pet: {type: cat,name: tom}
存放路径:src/main/resource目录或者类路径的/config
2.配置文件属性值的注入
2.1ConfigurationProperties注入属性
如果我们配置属性是SpringBoot的已有属性,这些属性SpringBoot会自动扫描识别的,对原来的值进行覆盖
如果配置的是一些自定义的属性,SpringBoot是不识别的,如果想让这些属性生效还需要对这些属性进行注入
使用@ConfigurationProperties注入属性 (走的是set方法)
相关注解:
在自定义类上加上如下两个注解:让配置生效
- @Component //生成当前类的实例对象存到IOC容器中
- @ConfigurationProperties(prefix = “person”) //将配置文件中前缀为person的每个属性的值映射到当前类中的变量上
(配置文件中的key值要和自定义类中的属性名称保持一致,才能完成属性注入)
@Autowired //在容器中取出实例对象,完成属性注入
private Person person;
2.2使用@Value注入属性
@Value("${person.name}") //注入基本数据类型
private String name;
2.3两种注解的对比分析
JSR303数据效验
(@Validated注解实现参数分组校验)
1、首先还是先导包,导入pom文件。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
2.常用校验规则
3.测试
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Email;
@Component
@ConfigurationProperties(prefix = "user")
@Validated //引入springboot支持的数据校验规则
public class User {
@Email(message = "邮箱格式错误")//以邮箱的形式校验
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"email='" + email + '\'' +
'}';
}
}
3.SpringBoot自定义配置
3.1自定义配置文件的加载
自定义的配置文件SpringBoot是无法自动加载的,我们需要借助注解实现加载:@PropertySource(“classpath:test.properties”) // 指定自定义配置文件的位置和名称,,,,@Configuration //指定当前类为配置类 也可以使用@component注解代替
@Configuration //指定当前类为配置类 也可以使用@component注解代替
@PropertySource("classpath:test.properties") // 指定自定义配置文件的位置和名称
//开启配置类的属性注入功能(使用@Configuration的时候需要,,使用@Component的时候不需要)
@EnableConfigurationProperties(MyProperties.class)
@ConfigurationProperties(prefix = "test") //配置类中的前缀为test的映射属性注入
3.2使用@ImportResource加载XML配置文件
@ImportResource:指定XML文件的位置
让XML配置文件生效
-
项目启动类上添加@ImportResource注解来指定XML文件位置
@ImportResource(“classpath:beans.xml”)
-
测试类中引入ApplicationContext实体类Bean,并新增一个测试方法进行输出测试
@Autowired
private ApplicationContext applicationContext;
3.3使用@Configuration编写自定义配置类
SpringBoot框架中并不推荐使用XML
中自定义配置文件,特殊的情况下才会用,,在SpringBoot框架中更推荐使用配置类
的方式向容器中添加和配置组件
@Configuration:定义一个配置类
@Bean:进行组件配置
@Configuration //指定当前类为配置类 也可以使用@Component注解代替
public class MyConfig {
@Bean(name = "myService1")//将标注方法的返回值存到 spring容器中 (相当于XML配置文件中bean标签)
public MyService myService(){
return new MyService();
}
}
4.Profile多环境配置
多环境配置方式:有两种
- Profile文件多环境配置
- @Profile注解多环境配置
方式一:Profile文件多环境配置
激活指定环境的方式:
1. 通过命令行方式激活指定环境的配置文件
先重新对项目打包
通过命令行
java --jar chapter02-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
问题解决
1.chapter02-0.0.1-SNAPSHOT.jar中没有主清单属性
2.原因可能是,你在处理某些问题的时候,不小心把 pom.xml 配置文件中的下面代码删除掉,然后打包执行,就出现了该问题
<build>
<plugins>
<!-- 加上这段代码 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.我们也可以看到没有上述代码打出来的包少了 demo-0.0.1-SNAPSHOT.jar.original
2.在全局配置文件设置spring.profiles.active属性激活
方式二:@Profile注解多环境配置
@Configuration
@Profile(“dev”) //指定多环境配置类的标识
5.随机值设置以及参数间引用
<!--引入SpringBoot依赖
1.统一进行版本控制
2.让当前项目具有springBoot特性
3.加载指定的配资文件
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<!--引入Web场景依赖启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>