【Spring Boot】Day03

一、@Value和@ConfigurationProperties的区别

区别:

  • 数据校验:判断数据是否合法
    • @Value: 不支持数据校验
    • @ConfigurationProperties:支持数据校验
      开启数据校验功能:
      1. 类上添加:@Validated
      2. 在属性上添加特定的校验注解 @Email

注意1:在使用@Email中,需要导入一个依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

注意2:如果使用了@Value,就不需要@ConfigurationProperties,程序就不知道properties里面写的什么,只是@Value赋值的就会显示,没赋值的就不会显示;两种方式只会顾着一边。

package com.zy.springboot.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
/***
 *
 * 读取配置文件中的数据,映射到此类的同名属性
 * @ConfigurationProperties
 *  prefix = "person": 读取person节点的数据
 *  @Component:让spring容器管理此类
 *
 *@Value("${person.name}") 参数名字必须和配置文件中的一样,即可跳转
 *
 * 数据校验:判断数据是否合法
 *  @Value: 不支持数据校验
 *  @ConfigurationProperties:支持数据校验
 *       开启数据校验功能:
 *           1. 类上添加:@Validated
 *           2. 在属性上添加特定的校验注解
 *                 @Email
 */

@Validated
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
//    @Value("李四")
    @Email(message = "不是邮箱格式")//校验邮箱
    private String name;
//    @Value("20")
    private Integer age;
//    @Value("${person.birth}")  //不需要使用ConfigurationProperties,会自动识别person
    private Date birth;
//    @Value("${person.b}")
    private Boolean b;
    private Map<String,String> maps;
    private List<String> lists;
    private Dog dog;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Boolean getB() {
        return b;
    }

    public void setB(Boolean b) {
        this.b = b;
    }

    public Map<String, String> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }

    public List<String> getLists() {
        return lists;
    }

    public void setLists(List<String> lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birth=" + birth +
                ", b=" + b +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

会显示结果:
在这里插入图片描述

二、@PropertySource

区别:

  • @ConfigurationProperties(prefix = “person”)是从全局配置文件中读取
    @PropertySource(“classpath:jdbc.properties”)可以指定配置文件

创建jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/examsystem
jdbc.username=root
jdbc.password=12345

创建JDBCProperties类

package com.zy.springboot.bean;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/*
*
* @ConfigurationProperties(prefix = "person")是从全局配置文件中读取
* @PropertySource("classpath:jdbc.properties")可以指定配置文件
*
*
* */
@Component
@PropertySource("classpath:jdbc.properties")
public class JDBCProperties {

    @Value("${jdbc.driverClassName}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public String toString() {
        return "JDBCProperties{" +
                "driverClassName='" + driverClassName + '\'' +
                ", url='" + url + '\'' +
                '}';
    }
}

测试

@Resource
	JDBCProperties jdbcProperties;

	@Test
	public void getProperties(){
		System.out.println(jdbcProperties);
	}

结果显示
在这里插入图片描述

三、切换不同环境的配置

1. 不同配置文件

  • 正式环境 开发环境
  • 两个开发环境的数据库不一致
  • 设置启用的配置文件
    在这里插入图片描述

application.properties

spring.profiles.active=prod

application-dev.properties

server.port=8888

application-prod.properties

server.port=9999

演示

package com.zy.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Springboot02Application {

	public static void main(String[] args) {
		SpringApplication.run(Springboot02Application.class, args);
	}

}

在这里插入图片描述

2. yml文档块

  • 实际项目测试中,需要不同的环境,例如:测试环境、运行环境、正式环境等等,不同的环境对应着不同的端口号,如何快速的转换环境,可通过配置文件来设定。
  • yml文件通过多文档块的方式配置不同的环境。
  • 每个文档块通过【- - -】隔离生成
spring:
  profiles:
    active: prod

---
server:
  port: 8888
spring:         //此为最新形式
  config:
    activate:
    on-profile: dev
---
server:
  port: 9999
spring:
  config:
    activate:
      on-profile: prod

四、配置文件加载顺序

按照优先级从高到低的顺序,所有的配置文件都会被加载,优先级的配置会覆盖低优先级的配置内容。

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值