Spring Cloud之Spring EL(2)
来源:互联网 发布:java实现发布订阅模式 编辑:程序博客网 时间:2024/05/21 16:59
3、Spring EL 与第三方信息
3.1、Spring EL 与 集合
Spring EL基本表达式可以对集合中的信息进行读取操作,包括Collection、Map性质的集合。请看如下实例:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
以上代码我们在myCollection这个bean被Spring容器初始化时,向bean中两种类型的集合属性写入了一些初始化值。接下来,我们通过@Value关键字,在依赖于这个bean的其它bean实例中,对属性进行赋值:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
这样一来,“value1”属性将得到“数据1”这个初始值,“value2”属性将得到“value1”这个初始化值。当然,在以上给出的“对myCollection bean进行初始化”的代码中,Spring XML还给出了一种配置方式,只是本专题中主要讲解是基于Spring boot的,所以就没有过多讲解Spring XML中的写法,这里给出补充的内容(通过XML进行myCollection bean中属性的初始化赋值):
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
3.2、Spring EL 与 配置(环境)信息
Spring EL 可以通过“$”关键字取得操作系统环境变量信息、应用程序运行时信息,以及应用程序配置文件中的信息。其中的原理是Spring中实现的某个配置信息获取配置器“org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”和“org.springframework.context.support.PropertySourcesPlaceholderConfigurer”。前者为Spring 3.1之前的版本提供,而Spring 3.1之后的版本多采用后者(但是前者PropertyPlaceholderConfigurer并没有被放弃使用,详细情况读者可参见这两个类的官方注释)。
接下来我们来看看Spring EL通过“$”关键字如何取得操作系统环境变量信息、应用程序运行时信息,以及应用程序配置文件中的信息:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
请注意,我们以上代码使用的示例环境为Spring Boot。后者为我们节省了大部分配置工作,例如我们可以直接将应用程序相关配置信息放置在规定的application.yml或者application.properties文件中,而无需在某个application.xml中指定配置文件。那么我们怎么知晓哪些操作系统环境变量信息、应用程序运行时信息可以通过“$”关键字进行加载呢?
- 操作系统环境变量信息就是我们在Linux操作系统中(以CentOS操作系统为例),通过env命令能够获取到的那些环境变量信息。在java语言中我们可以通过以下代码将这些信息加载出来:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
以下输出的调试信息,是笔者在自己的代码开发环境下得到的环境变量信息结果(由于篇幅原因,省去了部分):
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 而应用程序的运行时信息,是在应用程序运行过程中动态生成的信息。在java环境中,这些信息可以通过以下代码获得:
- 1
- 2
- 3
- 4
而在这个方法的官方注释中,也对这些应用程序运行时信息进行了部分说明(实际上根据操作系统的不同,信息还可能更多),参见以下的引用信息:
java.version: Java Runtime Environment version
java.vendor: Java Runtime Environment vendor
java.vendor.url: Java vendor URL
java.home: Java installation directory
java.vm.specification.version: Java Virtual Machine specification version
java.vm.specification.vendor: Java Virtual Machine specification vendor
java.vm.specification.name: Java Virtual Machine specification name
java.vm.version: Java Virtual Machine implementation version
java.vm.vendor: Java Virtual Machine implementation vendor
java.vm.name: Java Virtual Machine implementation name
java.specification.version: Java Runtime Environment specification version
java.specification.vendor: Java Runtime Environment specification vendor
java.specification.name: Java Runtime Environment specification name
java.class.version: Java class format version number
java.class.path: Java class path
java.library.path: List of paths to search when loading libraries
java.io.tmpdir: Default temp file path
java.compiler: Name of JIT compiler to use
java.ext.dirs: Path of extension directory or directories Deprecated. This property, and the mechanism which implements it, may be removed in a future release.
os.name: Operating system name
os.arch: Operating system architecture
os.version: Operating system version
file.separator: File separator (“/” on UNIX)
path.separator: Path separator (“:” on UNIX)
line.separator: Line separator (“\n” on UNIX)
// 这个属性已经演示过,代表启动当前应用程序的登录用户名
user.name: User’s account name
// 这个属性也已经演示过,代表启动当前应用程序的用户,所使用的主目录
user.home: User’s home directory
// 这个属性也已经演示过,代表启动当前应用程序的用户的工作目录
user.dir: User’s current working directory
========================
- Spring Cloud之Spring EL(2)
- Spring Cloud之Spring EL(1)
- Spring Cloud之Spring EL和AOP(3)
- Spring/Boot/Cloud系列知识(6)——Spring EL(2)
- Spring Cloud 2:Spring Boot之简介
- Spring Cloud之 Spring Cloud Config(一)
- Spring Cloud 之Spring Cloud Bus(二)
- Spring Cloud 之 Spring Cloud Eureka(四)
- Spring Cloud 之 Spring Cloud Zuul(七)
- Spring Cloud入门之Spring Cloud Config
- spring cloud 之Spring Cloud Config 尝鲜
- Spring Cloud 之断路器
- Spring Cloud之架构
- Spring Cloud之ConfigServer
- Spring Cloud之Netflix
- spring cloud 之 zuul
- spring cloud 之 zuul
- spring cloud之eureka
- svn的使用说明
- CPU的CAS操作
- sparkstreaming和kafka0.10版本整合
- Python列表;字符串;字典等常用操作
- Pandas的read_csv函数参数分析
- Spring Cloud之Spring EL(2)
- Virtualbox主机和虚拟机之间文件夹共享及双向拷贝
- 2017数学建模B题回顾与解题分享
- 开启微信公众号自动回复
- 手机滑动解锁
- 解决方法:Mac Android studio 创建项目是下载gradle一直不返回
- KEIL中如何程序在 RAM 中运行
- 车架号识别系统-VIN码识别查询
- Python: 定制类