@Value and @ConfigurationProperties get configuration information

table of Contents

Preface

Configuration read

The difference between "#{}" and "${}" in @Value annotation


Preface

In a java project, we can obtain and bind the information in the configuration file in the following common ways:

  • @Value annotation
  • Spring's Environment class (available through interface or direct injection, and then use the getProperty() method)
  • @ConfigurationProperties annotation (used under SpringBoot)

@ConfigurationProperties is an annotation provided by Springboot to read the configuration file. It is mainly used to map the value of each property in the configuration file to the current class. The identified class must be placed in the component of the container, so the @Component annotation is also required Or @Configuration annotation

@Value is an annotation to get the configuration file under Spring. If the corresponding configuration item cannot be found when the annotation is read and the default value is not set, an error will be reported

This article simply records the use of these two annotations

Configuration read

The @ConfigurationProperties configuration file is as follows:

# @ConfigurationProperties注解配置
# String 配置读取
config.test.strV=stringTest
# int 配置读取
config.test.intV=4
# boolean 配置读取
config.test.bolV=true
# list 两种配置读取方式
config.test.list=zhangSan, liSi
#config.test.list[0]=zhangSan
#config.test.list[1]=liSi

# map(bean同理) 两种配置读取方式
config.test.map.k1=v1
config.test.map.k2=v2
config.test.map.k3=v3
#config.test.map[k1]=v1
#config.test.map[k2]=v2
#config.test.map[k3]=v3

The @Value configuration file is as follows:

# @Value注解配置
# String 配置读取
config.value.strV=stringTest
# int 配置读取
config.value.intV=4
# boolean 配置读取
config.value.bolV=true
# list 配置读取,主要是通过分隔符进行分割
config.value.list=zhangSan,liSi

# map 配置读取
config.test.map={k1:'v1', k2:'v2', k3:'v3'}

Configuration file entity class:

@Configuration
@ConfigurationProperties(prefix = "config.test")
public class ConfigTest {

    private String strV;
    private Integer intV;
    private boolean bolV;
    private List<String> list;
    private Map<String, String> map;

    @Override
    public String toString() {
        return "ConfigTest{" +
                "strV='" + strV + '\'' +
                ", intV=" + intV +
                ", bolV=" + bolV +
                ", list=" + list +
                ", map=" + map +
                '}';
    }

}

Test category:

@SpringBootTest
@RunWith(SpringRunner.class)
public class ConfigTestTest {

    @Autowired private ConfigTest configTest;

    @Value("${config.value.strV:defalutStringValue}") private String strV;
    @Value("${config.value.intV:8}") private Integer intV;
    @Value("${config.value.bolV:false}") private boolean bolV;
    @Value("#{'${config.value.list:null}'.split(',')}") private List<String> list;
    @Value("#{${config.test.map}}") private Map<String, String> map;

    @Test
    public void test() {
        // @ConfigurationProperties 注解测试
        System.out.println(configTest.toString());
        System.out.println("------------");
        // @Value 注解测试
        System.out.println("strV=" + strV);
        System.out.println("intV=" + intV);
        System.out.println("bolV=" + bolV);
        System.out.println("list=" + list);
        System.out.println("map=" + map);
    }
}

The output is as follows:

ConfigTest{strV='stringTest', intV=4, bolV=true, list=[zhangSan, liSi], map={k1=v1, k2=v2, k3=v3}}
------------
strV=stringTest
intV=4
bolV=true
list=[zhangSan, liSi]
map={k1=v1, k2=v2, k3=v3}

The difference between "#{}" and "${}" in @Value annotation

@Value("#{}"): SpEL expression, usually used to obtain the properties of the bean, or to call a method of the bean. Of course there are constants that can be expressed

@Value("${}"): You can get the attribute value defined in the corresponding attribute file

Guess you like

Origin blog.csdn.net/m0_38001814/article/details/113033250