@ConfigurationProperties(prefix=" ")
package com.atguigu.springboot.bean;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
}
@Component也要加在上面,@ConfigurationProperties才能生效。
@ConfigurationProperties具有校验功能,比如加个@Email则说输入必须为邮箱格式(类前面还要加@Validated)。
@ConfigurationProperties注解下,last-name,last_name,lastName等价。
而@Value不具备上述两个功能,不支持校验,不支持大小写那个语法。
@Value("${person.last-name}")//从注解获取last-name的值
private String lastName;
@Value("#{11*2}")//给age注入22
private Integer age;
@Value("true")给boss注入true
private Boolean boss;
@Value只能取出基本类型数据,不能取复杂类型,如map。@ConfigurationProperties支持。
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们直接使用@ConfigurationProperties。