当前位置: 首页 > news >正文

未明潮网站建设保密协议做网站怎样申请动态域名

未明潮网站建设保密协议,做网站怎样申请动态域名,网站建设招聘岗位,网站搭建中企动力最行Spring中的Value注解详解 概述 本文配置文件为yml文件 在使用spring框架的项目中#xff0c;Value是经常使用的注解之一。其功能是将与配置文件中的键对应的值分配给其带注解的属性。在日常使用中#xff0c;我们常用的功能相对简单。本文使您系统地了解Value的用法。 Value…Spring中的Value注解详解 概述 本文配置文件为yml文件 在使用spring框架的项目中Value是经常使用的注解之一。其功能是将与配置文件中的键对应的值分配给其带注解的属性。在日常使用中我们常用的功能相对简单。本文使您系统地了解Value的用法。 Value 注解可以用来将外部的值动态注入到 Bean 中在 Value 注解中可以使${} 与 #{} 它们的区别如下 1Value(“${}”)可以获取对应属性文件中定义的属性值。 2Value(“#{}”)表示 SpEl 表达式通常用来获取 bean 的属性或者调用 bean 的某个方法。 使用方式 根据注入的内容来源 Value属性注入功能可以分为两种通过配置文件进行属性注入和通过非配置文件进行属性注入。 非配置文件注入的类型如下 注入普通字符串注入操作系统属性注入表达式结果注入其他bean属性注入URL资源 基于配置文件的注入 首先让我们看一下配置文件中的数据注入无论它是默认加载的application.yml还是自定义my.yml文档需要PropertySource额外加载。 application.yml文件配置获得里面配置的端口号 程序源代码 package cn.wideth.controller;import cn.wideth.PdaAndIpadApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner;RunWith(SpringRunner.class) SpringBootTest() ContextConfiguration(classes PdaAndIpadApplication.class) public class ValueController {/***Get in application.yml*/Value(${server.port})private String port;Testpublic void getPort(){System.out.println(port);} }程序结果 自定义yml文件application-config.yml文件配置获得里面配置的用户密码值 注意如果想导入自定义的yml配置文件应该首先把自定义文件在application.yml文件中进行注册自定义的yml文件要以application开头形式为application-fileName 配置信息 测试程序 package cn.wideth.controller;import cn.wideth.PdaAndIpadApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner;RunWith(SpringRunner.class) SpringBootTest() ContextConfiguration(classes PdaAndIpadApplication.class) public class ValueController {/***Get in application-config.yml*/Value(${user.password})private String password;Testpublic void getPassword(){System.out.println(password);} }程序结果 基于配置文件一次注入多个值 配置信息 测试程序 package cn.wideth.controller;import cn.wideth.PdaAndIpadApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner;import java.util.List;RunWith(SpringRunner.class) SpringBootTest() ContextConfiguration(classes PdaAndIpadApplication.class) public class ValueController {/***Injection array (automatically split according to ,)*/Value(${tools})private String[] toolArray;/***Injection list form (automatic segmentation based on , and)*/Value(${tools})private ListString toolList;Testpublic void getTools(){System.out.println(toolArray);System.out.println(toolList);} }程序结果 基于非配置文件的注入 在使用示例说明基于非配置文件注入属性的实例之前让我们看一下SpEl。 Spring Expression Language是Spring表达式语言可以在运行时查询和操作数据。使用{…}作为操作符号大括号中的所有字符均视为SpEl。 让我们看一下特定实例场景的应用 注入普通字符串 测试程序 package cn.wideth.controller;import cn.wideth.PdaAndIpadApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner;import java.util.List;RunWith(SpringRunner.class) SpringBootTest() ContextConfiguration(classes PdaAndIpadApplication.class) public class ValueController {// 直接将字符串赋值给 str 属性Value(hello world)private String str;Testpublic void getValue(){System.out.println(str);} }程序结果 注入操作系统属性 可以利用 Value 注入操作系统属性。 测试程序 package cn.wideth.controller;import cn.wideth.PdaAndIpadApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner;RunWith(SpringRunner.class) SpringBootTest() ContextConfiguration(classes PdaAndIpadApplication.class) public class ValueController {Value(#{systemProperties[os.name]})private String osName; // 结果Windows 10Testpublic void getValue(){System.out.println(osName);} }程序结果 注入表达式结果 在 Value 中允许我们使用表达式然后自动计算表达式的结果。将结果复制给指定的变量。如下 测试程序 package cn.wideth.controller;import cn.wideth.PdaAndIpadApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner;RunWith(SpringRunner.class) SpringBootTest() ContextConfiguration(classes PdaAndIpadApplication.class) public class ValueController {// 生成一个随机数Value(#{ T(java.lang.Math).random() * 1000.0 })private double randomNumber;Testpublic void getValue(){System.out.println(randomNumber);} }程序结果 注入其他bean属性 其他Bean package cn.wideth.controller;import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;//其他bean自定义名称为 myBeans Component(myBeans) public class OtherBean {Value(OtherBean的NAME属性)private String name;public String getName() {return name;}public void setName(String name) {this.name name;} }测试程序 package cn.wideth.controller;import cn.wideth.PdaAndIpadApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner;RunWith(SpringRunner.class) SpringBootTest() ContextConfiguration(classes PdaAndIpadApplication.class) public class ValueController {Value(#{myBeans.name})private String fromAnotherBean;Testpublic void getValue(){System.out.println(fromAnotherBean);} }程序结果 注入URL资源 测试程序 package cn.wideth.controller;import cn.wideth.PdaAndIpadApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import java.net.URL;RunWith(SpringRunner.class) SpringBootTest() ContextConfiguration(classes PdaAndIpadApplication.class) public class ValueController {/***注入 URL 资源*/Value(https://www.baidu.com/)private URL homePage;Testpublic void getValue(){System.out.println(homePage);} } 程序结果
http://www.w-s-a.com/news/52162/

相关文章:

  • 沈阳市建设局网站sem优化师是什么意思
  • 餐饮vi设计公司网站排名优化方法讲解
  • 无线昆明官方网站可以做书的网站
  • 信誉最好的20个网投网站凡科网站建设之后怎么删除
  • 天津网站开发技术广州网站优化公司排名
  • 养老做增减的网站医院网站怎么做优化排名
  • 企业网站的推广方法有哪些上海猎头公司前十名
  • 电商网站建设建议免费下载app
  • 网站搭建设计是什么意思百度地图放到网站上
  • 东莞网站建设市场分析淘宝网站框架
  • 新网站多久被百度收录网站空间单位
  • 2017常用的网站昆明网站代理
  • 成都海鸥手表网站安阳网站建设策划
  • 做好的网站怎么发布做网站应该做哪方面的
  • 可以找厂家的网站品牌创意型网站开发
  • 有没有做牛羊角的网站电商网站报价
  • 网站建设行业咨讯文章网站兼容模式怎么设置
  • 商务网站建设概念东莞做网站的公司吗
  • 高稳定性的网站设计制作wordpress 检测插件
  • 无锡网站制作排名自适应网站建设推荐
  • 度娘网站桃花怎么做网站制作 p
  • 小欢喜林磊儿什么网站做家教搜索优化公司
  • 龙岗做网站哪里找网站建设简介是什么意思
  • 做网站的标准北京西站出站口
  • asp.net新建网站市场营销管理是做什么的
  • 南昌网站建设模板服务商建设什么网站挣钱
  • 网站建设实训记录企业网站建设运营
  • 视频网站文案住房和城乡建设部门
  • 汕头网站排名推广新余门户网站开发
  • 湖南智能网站建设哪家好wordpressμ