What is the way SpringBoot injects data?
本篇内容介绍了"SpringBoot注入数据的方式是什么"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
application.properties
boot.name=liqingfengboot.age=28boot.pswd=123456@RunWith(SpringRunner.class)@SpringBootTestpublic class DemoApplicationTests { @Value("${boot.name}") private String name; @Value("${boot.pswd}") private Integer pswd; @Value("${boot.age}") private Integer age;}@Component@ConfigurationProperties(prefix = "boot")@Datapublic class User { private String name; private Integer age; private Integer pswd;}@Autowiredprivate Environment env;@RunWith(SpringRunner.class)@SpringBootTestpublic class DemoApplicationTests { @Autowired private Environment env; @Test public void contextLoads() { System.out.println("名字:" + env.getProperty("boot.name")+ "年龄:" + env.getProperty("boot.age") +"密码:" + env.getProperty("boot.pswd")); }} @Value("注入普通字符串") private String normal; //关于属性的KEY可以查看System类说明 @Value("#{systemProperties['java.version']}")//-->使用了SpEL表达式 private String systemPropertiesName; // 注入操作系统属性 @Value("#{T(java.lang.Math).random()*80}")//获取随机数 private double randomNumber; //注入表达式结果 @Value("#{1+2}") private double sum; //注入表达式结果 1+2的求和 @Value("classpath:os.yaml") private Resource resourceFile; // 注入文件资源 @Value("http://www.baidu.com") private Resource testUrl; // 注入URL资源 @Value("#{st.name}") private String studentName;#{...}和${...}的区别演示A.${…}的用法{}里面的内容必须符合SpEL表达式,通过@Value("${app.name}")可以获取属性文件中对应的值,但是如果属性文件中没有这个属性,则会报错。可以通过赋予默认值解决这个问题,如@Value("${app.name:胖先森}")// 如果属性文件没有app.name,则会报错// @Value("${app.name}")// private String name;// 使用app.name设置值,如果不存在则使用默认值@Value("${app.name:胖先森}")private String name;B.#{...}的用法// SpEL:调用字符串Hello World的concat方法@Value("#{'Hello World'.concat('!')}")private String helloWorld;// SpEL: 调用字符串的getBytes方法,然后调用length属性@Value("#{'Hello World'.bytes.length}")private String helloWorldbytes;C.#{...}和${...}混合使用${...}和#{...}可以混合使用,如下文代码执行顺序:通过${server.name}从属性文件中获取值并进行替换,然后就变成了 执行SpEL表达式{'server1,server2,server3'.split(',')}。// SpEL: 传入一个字符串,根据","切分后插入列表中, #{}和${}配置使用(注意单引号,注意不能反过来${}在外面,#{}在里面)@Value("#{'${server.name}'.split(',')}")private List servers;在上文中在#{}外面,${}在里面可以执行成功,那么反过来是否可以呢${}在外面,#{}在里面,如代码// SpEL: 注意不能反过来${}在外面,#{}在里面,这个会执行失败@Value("${#{'HelloWorld'.concat('_')}}")private List servers2;答案是不能。因为spring执行${}是时机要早于#{}。在本例中,Spring会尝试从属性中查找#{'HelloWorld'.concat('_')},那么肯定找到,由上文已知如果找不到,然后报错。所以${}在外面,#{}在里面是非法操作D.用法总结#{…} 用于执行SpEl表达式,并将内容赋值给属性${…} 主要用于加载外部属性文件中的值#{…} 和${…} 可以混合使用,但是必须#{}外面,${}在里面${random.value}、${random.int}、${random.long}${random.int(10)}、${random.int[1024,65536]}@Value获取值和@ConfigurationProperties获取值比较
Part code for data validation
@Component@ConfigurationProperties(prefix = "person")@Validatedpublic class Person { //lastName must be in mailbox format @Email private String lastName;"What is the way SpringBoot injects data" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!