Spring使用@Value注解各种类型的值


在application.properties写入下面代码

test.boolean=true
test.string=abc
test.integer=123
test.long=123
test.float=1.2345678123456
test.double=1.2345678123456
test.array=1,3,4,5,6,1,2,3
test.list=1,3,4,5,6,1,2,3
test.set=1,3,4,5,6,1,2,3
test.map={name:"张三", age:18}
 

使用junit进行测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ApplicationBootstrap.class})
public class DemoTest {

@Value("${test.boolean}")
private Boolean testBoolean;

@Value("${test.string}")
private String testString;

@Value("${test.integer}")
private Integer testInteger;

@Value("${test.long}")
private Long testLong;

@Value("${test.float}")
private Float testFloat;

@Value("${test.double}")
private Double testDouble;

@Value("#{'${test.array}'.split(',')}")
private String[] testArray;

@Value("#{'${test.list}'.split(',')}")
private List<String> testList;

@Value("#{'${test.set}'.split(',')}")
private Set<String> testSet;

@Value("#{${test.map}}")
private Map<String, Object> testMap;

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM