• Spring EL 表达式实现资源的注入


    前言

    Spring 开发中经常涉及调用各种资源的情况,包括普通文件,网址,配置文件,环境变量。使用spring 表达式语言实现资源的注入 ,能够以一种强大和简单的方式将值装配到bean 属性和构造器中.

    特性

    spring EL 的特性

    • 使用bean 的id 来引用bean
    • 调用方法和访问对象的属性
    • 对值进行算术,关系 和逻辑运算
    • 正则表达式匹配
    • 集合操作

    使用

    spring主要在@value 的参数中使用表达式,@value可注入以下参数:

    1. 普通字符

    @Value(12)
    private String str;
    

    2.操作系统属性

    @Value("#{systemProperties['os.name']}")
    private String osName;
    @Value("#{systemProperties['os.name']}")
    private static String osName;
    

    3.表达式运算结果

    @Value("#{T(java.lang.Math).random() * 100.0}") //随机数计算值
    private Double randomDouble;
    

    4.其他bean的属性

    @Value("#{UserServer.name}")
    

    5.文件内容

    @Value(“classpath:test.txt”)
    private Resource testFile;
    

    6.网址内容

    @Value(“http://www.baidu.com”)
    private Resource testUrl;
    

    7.属性文件

    @value("${book.name}")
    private String bookName;
    

    测试

    package com.blog.www.config;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.core.io.Resource;
    import org.springframework.stereotype.Component;
    
    /**
     * SpringBootEL表达式注入测试
     * <br/>
     *
     * @author :leigq
     * @date :2019/8/21 14:42
     */
    @Component
    public class SpringELAutoWired {
    
    	/**
    	 * 普通字符 name = "this is name"
    	 */
    	@Value("this is name")
    	private String name;
    
    	/**
    	 * 操作系统属性
    	 */
    	@Value("#{systemProperties['os.name']}")
    	private String osName;
    
    	/**
    	 * 表达式运算结果
    	 */
    	@Value("#{T(java.lang.Math).random() * 100.0}")
    	private Double randomDouble;
    
    	/**
    	 * 其他bean的属性 {@link I18nConfig} 被 spring 管理,spring 注入是类名首字母小写
    	 */
    	@Value("#{i18nConfig.ZH_CN}")
    	private String otherBeanProperties;
    
    	/**
    	 * 文件内容
    	 */
    	@Value("classpath:banner.txt")
    	private Resource banner;
    
    	/**
    	 * 网址内容
    	 */
    	@Value("http://www.baidu.com")
    	private Resource webUrl;
    
    	/**
    	 * 属性文件
    	 */
    	@Value("${spring.profiles.active}")
    	private String propertiesName;
    
    
    	public void test() {
    		System.out.println(name);
    		System.out.println(osName);
    		System.out.println(randomDouble);
    		System.out.println(otherBeanProperties);
    		System.out.println(banner);
    		System.out.println(webUrl);
    		System.out.println(propertiesName);
    	}
    
    }
    
    
    package com.blog.www;
    
    import com.blog.www.base.BaseApplicationTests;
    import com.blog.www.config.SpringELAutoWired;
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.core.io.Resource;
    
    /**
     * SpringBootEL表达式注入测试
     * <br/>
     *
     * @author :leigq
     * @date :2019/8/21 14:42
     */
    public class SpringELAutoWiredTest extends BaseApplicationTests {
    
    	@Autowired
    	private SpringELAutoWired springELAutoWired;
    
    	@Test
    	public void test() {
    		springELAutoWired.test();
    	}
    
    }
    
    

    测试结果:

    在这里插入图片描述


    作者:不敲代码的攻城狮
    出处:https://www.cnblogs.com/leigq/
    任何傻瓜都能写出计算机可以理解的代码。好的程序员能写出人能读懂的代码。

     
  • 相关阅读:
    [Unity3D]计时器/Timer
    unity缓存和浏览器缓存
    unity3d进行脚本资源打包加载
    Unity3d删除无用的美术资源
    项目经理的职责(转载)
    LINQ
    生意经
    Android ListView标题置顶效果实现
    ListView的自动循环滚动显示
    郭霖的专栏
  • 原文地址:https://www.cnblogs.com/leigq/p/13406528.html
Copyright © 2020-2023  润新知