Spring EL表示式的运用@Value说明
作者:xiaolyuh123 发布时间:2023-03-05 02:33:19
标签:Spring,EL,@Value
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&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai</jdbc-url>
<lcn-log-url>jdbc:mysql://127.0.0.1:3306/tx-manager?allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&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}
来源:https://blog.csdn.net/xiaolyuh123/article/details/61924447


猜你喜欢
- Android 中 Tweened animation的实例详解Tweened animation有四种类型,下面主要介绍Scale类型。运
- 说到Java的本地存储,肯定使用IO流进行操作。首先,我们需要一个创建文件的函数createNewFile:public static bo
- 缓存可以说是加速服务响应速度的一种非常有效并且简单的方式。在缓存领域,有很多知名的框架,如EhCache 、Guava、HazelCast等
- C#提升管理员权限修改本地Windows系统时间在桌面应用程序开发过程中,需要对C盘下进行文件操作或者系统参数进行设置,例如在没有外网的情况
- 概述Spring针对Java Transaction API (JTA)、JDBC、Hibernate和Java Persistence A
- 一、概述EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在F
- 前言微信群里的一个提问引发的这个问题,C#异步有多少种实现方式?首先想要知道C#异步有多少中实现方式,首先我们要知道.NET提供的执行异步操
- Android 滑动监听的实例详解摘要: ScollBy,ScollTo是对内容的移动,view.ScollyBy是对view的内容的移动&
- 一、二叉搜索树插入元素/** * user:ypc; * date:2021-05-18; * time: 15:09; */
- 可重入锁,从字面来理解,就是可以重复进入的锁。可重入锁,也叫做递归锁,指的是同一线程外层函数获得锁之后,内层递归函数仍然有获取该锁的代码,但
- 一. 简介 俩个数据库db1,db2, db1数据库的map
- 偶然发现有小伙伴错误地使用了Collections.emptyList()方法,这里记录一下。她的使用方式是:public void run
- 个人详情页滑动到顶部最近产品提了个新需求,需要实现点击App内的某个按钮跳转到个人详情页并且滑动到顶部,个人详情页的页面交互稍微复杂,技术角
- layout布局<RelativeLayout xmlns:android="http://schemas.android.
- 1.类的6个默认成员函数默认成员函数:用户没有显示实现,编译器会生成的成员函数称为默认成员函数。如果一个类中什么成员都没有,简称为空类。但空
- 本文实例为大家分享了C#生成唯一订单号的具体代码,供大家参考,具体内容如下根据GUID+DateTime.Now.Ticks生产唯一订单号/
- JFormDesigner概述jformdesigner是一款功能强大的Swing设计工具,这是全面的程序,可帮助您创建Swing GUI,
- 如下就可以获取button中的文本内容using System.Collections.Generic;using UnityEngine;
- 这篇文章需要对git具有一定的了解,并且知道如何安装git工具(其实就是上git官网下载个文件,安装)git这种版本控制工具有什么好处第一个
- 前面有文章介绍了使用GridView实现表格的方法,本文就来说说如何用ListView实现自适应的表格。GridView比ListView更