what?
一个注解,可以通过配置为系统中变量注入值。
how?
在对象属性使用@Value注解获取配置值
package mabatispluslearn;
import mabatispluslearn.com.wang.mp.config.UserConfigProperties;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
/**
* @author zhipan
* {@code @Value}的使用
*/
@SpringBootTest
public class ValueTest {
//${} 从配置文件中取值
@Value("${wang.value.name}")
private String username;
@Resource()
UserConfigProperties userConfigProperties;
//#{} spEL表达式,可以从容器对象的属性值取值,并进行计算
@Value("#{userConfigProperties.age+ 20}")
private Integer age;
@Test
void test(){
System.out.println("配置值获取==>username");
System.out.println(username);
System.out.println("配置值获取==>age");
System.out.println(age);
}
}
测试效果:
配置值:
获取值输出: