文章目录
- 一、@Value和@ConfigurationProperties的区别
 - 二、@PropertySource
 
一、@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);
	}
 
结果显示
 

![[附源码]java毕业设计价格公示系统](https://img-blog.csdnimg.cn/5d748d427b4e4b659df64a7b188738a5.png)

















