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

北京微信网站建设报价上海搜索关键词排名

北京微信网站建设报价,上海搜索关键词排名,dw怎么做百度页面网站,高密市建设局网站Spring中的Value注解详解 概述 本文配置文件为yml文件 在使用spring框架的项目中,Value是经常使用的注解之一。其功能是将与配置文件中的键对应的值分配给其带注解的属性。在日常使用中,我们常用的功能相对简单。本文使您系统地了解Value的用法。 Value…

Spring中的@Value注解详解

概述

本文配置文件为yml文件

在使用spring框架的项目中,@Value是经常使用的注解之一。其功能是将与配置文件中的键对应的值分配给其带注解的属性。在日常使用中,我们常用的功能相对简单。本文使您系统地了解@Value的用法。

@Value 注解可以用来将外部的值动态注入到 Bean 中,在 @Value 注解中,可以使${} 与 #{} ,它们的区别如下:

(1)@Value(“${}”):可以获取对应属性文件中定义的属性值。
(2)@Value(“#{}”):表示 SpEl 表达式通常用来获取 bean 的属性,或者调用 bean 的某个方法。

使用方式

根据注入的内容来源,@ Value属性注入功能可以分为两种:通过配置文件进行属性注入和通过非配置文件进行属性注入。
非配置文件注入的类型如下:

  1. 注入普通字符串
  2. 注入操作系统属性
  3. 注入表达式结果
  4. 注入其他bean属性
  5. 注入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 List<String> 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 10@Testpublic  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.15wanjia.com/news/56339.html

相关文章:

  • wordpress文章显示软件下载上海seo网站优化
  • 色情网站制作企业如何网络推广
  • 办公用品网站建设策划书谷歌搜索引擎免费入口 香港
  • 重庆网站建设是什么怎么做网站优化排名
  • 大连网站建设设计专业排名优化工具
  • 湖北建科建设工程有限公司网站google国际版
  • 江门网络营销厦门专业做优化的公司
  • 沈阳响应式网站建设百度电话查询
  • 做一婚恋网站多少钱上海seo公司
  • 深圳网站建设手机网站建设销售网站怎么做
  • 做电力招聘的有哪些网站百度推广公司电话
  • 网站建设流程包括哪些厦门seo百度快照优化
  • 网店装修定制沈阳seo关键词
  • 天津有哪些好的做网站公司创建网站需要什么条件
  • 企业网站策划书制作百度推广价格
  • 网站开发功能需求表成都百度推广排名优化
  • 电商网站有那些seo技术培训泰州
  • 商城类网站如何众筹百度推广代理公司
  • 找一家秦皇岛市做网站的公司重庆网站建设维护
  • 网站域名如何申请百度指数明星搜索排名
  • 做解析视频网站怎么赚钱热搜榜排名前十
  • 外贸网站建设推广优化网站软件推荐
  • asp.net网站改版 旧网站链接站长统计app最新版本2023
  • 音乐网站开发可行性分析电商网站模板
  • 做网站怎么插音乐济南seo网站关键词排名
  • wordpress上传数据库seo英文怎么读
  • 深圳网站开发哪个好短视频运营
  • wordpress创建网站微软优化大师
  • 前端开发是什么工作衡阳网站优化公司
  • 网站开发表格在线培训课程