当前位置: 移动技术网 > IT编程>开发语言>Java > Spring EL表示式的运用@Value说明

Spring EL表示式的运用@Value说明

2020年05月12日  | 移动技术网IT编程  | 我要评论
spring el表达式语言,支持在xml和注解中表达式,类是于jsp的el表达式语言。在spring开发中经常涉及调用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等,我们可以使用spr

spring el表达式语言,支持在xml和注解中表达式,类是于jsp的el表达式语言。

在spring开发中经常涉及调用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等,我们可以使用spring的表达式语言实现资源的注入。

spring主要在注解@value的参数中使用表达式。

本事咧演示一下情况:

注入普通字符串
注入操作系统属性
注入表达式运算结果
注入其他bean的属性
注入文件内容
注入网址内容
注入属性文件(注意:用的是$符号)

配置文件test.properties:

book.author=wangyunfei
book.name=spring boot

测试文件test.text:

你好!spring boot

注入类:

@configuration // 声明当前类是一个配置类,相当于spring配置的xml文件
// 包扫描,并排除了对beanconfig的扫描
@componentscan(basepackages={"com.chenfeng.xiaolyuh"}, excludefilters={@componentscan.filter(type=filtertype.assignable_type, value={beanconfig.class, aopconfig.class})})
@propertysource("classpath:test.properties")// 指定文件地址
public class elconfig {
 @value("注入普通字符串")// 注入普通字符串
 private string normal;
 
 @value("#{systemproperties['os.name']}")// 注入操作系统属性
 private string osname;
 
 @value("#{t(java.lang.math).random() * 100.0 }")// 注入表达式结果
 private double randomnumber; 
 
 @value("#{demoelservice.another}")// 注入其他bean属性
 private string fromanother;
 
 @value("classpath:test.txt")// 注入文件资源
 private resource testfile;
 
 @value("https://www.baidu.com")// 注入网址资源
 private resource testurl;
 
 @value("${book.name}")// 注入配置文件【注意是$符号】
 private string bookname;
 
 @autowired// properties可以从environment获得
 private environment environment;
 
// @bean
// public static propertysourcesplaceholderconfigurer propertyconfigure() {
// return new propertysourcesplaceholderconfigurer();
// }
 
 @override
 public string tostring() {
 try {
 return "elconfig [normal=" + normal 
  + ", osname=" + osname 
  + ", randomnumber=" + randomnumber 
  + ", fromanother=" + fromanother 
  + ", testfile=" + ioutils.tostring(testfile.getinputstream()) 
  + ", testurl=" + ioutils.tostring(testurl.getinputstream()) 
  + ", bookname=" + bookname
  + ", environment=" + environment.getproperty("book.name") + "]";
 } catch (ioexception e) {
 e.printstacktrace();
 return null;
 }
 }
 
}

测试类:

public class springeltest {
 annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(elconfig.class);
 
 @test
 public void contexttest() {
 elconfig elconfig = context.getbean(elconfig.class);
 system.out.println(elconfig.tostring());
 }
 
 @after
 public void closecontext() {
 context.close();
 }
 
}

补充知识:yml、properties获取pom自定义变量

pom变量:

<profiles>
 <profile>
  <!-- 本地环境 -->
  <id>dev</id>
  <properties>
   <profiles.env>dev</profiles.env>
   <jdbc-url>jdbc:mysql://127.0.0.1:3306/melab?allowmultiqueries=true&amp;useunicode=true&amp;characterencoding=utf-8&amp;servertimezone=asia/shanghai</jdbc-url>
   <lcn-log-url>jdbc:mysql://127.0.0.1:3306/tx-manager?allowmultiqueries=true&amp;useunicode=true&amp;characterencoding=utf-8&amp;servertimezone=asia/shanghai</lcn-log-url>
   <jdbc-user>root</jdbc-user>
   <jdbc-password>123456</jdbc-password>
  </properties>
 </profile>
</profiles>

yml获取pom变量:

添加依赖:

<!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml -->
<dependency>
 <groupid>org.yaml</groupid>
 <artifactid>snakeyaml</artifactid>
 <version>1.25</version>
</dependency>

获取变量:

url: @jdbc-url@
lcn-log-url: @jdbc-url@
username: @jdbc-user@
password: @jdbc-password@
properties获取pom变量:

build设置:

<build>
 <!--properties解析pom-->
 <pluginmanagement>
  <plugins>
   <plugin>
    <artifactid>maven-resources-plugin</artifactid>
    <configuration>
     <encoding>utf-8</encoding>
     <usedefaultdelimiters>true</usedefaultdelimiters>
    </configuration>
   </plugin>
  </plugins>
 </pluginmanagement>
</build>

获取变量:

spring.datasource.url=${jdbc-url}
spring.datasource.username=${jdbc-user}
spring.datasource.password=${jdbc-password}

以上这篇spring el表示式的运用@value说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网