05-SpringBoot——Spring常用配置-Spring EL和资源调用

来源:互联网 发布:淘宝网天猫阿兴家艾条 编辑:程序博客网 时间:2024/06/13 02:23

Spring常用配置-Spring EL和资源调用


【博文目录>>>】


【项目源码>>>】


【Spring EL和资源调用】


Spring EL-Spring 表达式语言,支持在xml和注解中使用表达式,类似于JSP 的EL 表达式语言。Spring 开发中经常涉及谓用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等,我们可以使用Spring 的表达式语言实现资源的注入。

Spring 主要在注解@Value 的参数中使用表达式,可以实现但不限于以下几种情况:

(1) 注入普通字符:(2) 注入操作系统属性:(3) 注入表达式运算结果;(4) 注入其他Bean 的属性:(5) 注入文件内容:(6) 注入网址内容:(7) 注入属性文件。

【代码实现】


package com.example.spring.framework.el;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Service;/** * Author: 王俊超 * Date: 2017-07-10 22:47 * All Rights Reserved !!! */@Servicepublic class DemoService {    @Value("其他类的属性") // 注入普通字符串    private String another;    public String getAnother() {        return another;    }    public void setAnother(String another) {        this.another = another;    }}
package com.example.spring.framework.el;import org.apache.commons.io.IOUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;import org.springframework.core.env.Environment;import org.springframework.core.io.Resource;/** * Author: 王俊超 * Date: 2017-07-10 22:47 * All Rights Reserved !!! */@Configuration@ComponentScan("com.example.spring.framework.el")@PropertySource("classpath:com/example/spring/framework/el/test.properties")public class ElConfig {    @Value("I Love You!") // 注入普通字符串    private String normal;    @Value("#{systemProperties['os.name']}") // 注入操作系统属性    private String osName;    @Value("#{T(java.lang.Math).random()*100.0}") // 注入表达式结果    private double randomNumber;    @Value("#{demoService.another}") // 注入其他Bean属性    private String fromAnother;    @Value("classpath:com/example/spring/framework/el/test.txt") // 注入文件资源    private Resource testFile;    @Value("http://www.baidu.com") // 注入网址资源    private Resource testUrl;    @Value("${book.name}") // 注入配置文件    private String bookName;    @Autowired    private Environment environment;    @Bean    public static PropertySourcesPlaceholderConfigurer propertyConfigure() {        return new PropertySourcesPlaceholderConfigurer();    }    public void outputResource() {        try {            System.out.println(normal);            System.out.println(osName);            System.out.println(randomNumber);            System.out.println(fromAnother);            System.out.println(IOUtils.toString(testFile.getInputStream(), "utf-8"));            System.out.println(IOUtils.toString(testUrl.getInputStream(), "utf-8"));            System.out.println(bookName);            System.out.println(environment.getProperty("book.author"));        } catch (Exception e) {            e.printStackTrace();        }    }}
package com.example.spring.framework.el;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Author: 王俊超 * Date: 2017-07-10 22:42 * All Rights Reserved !!! */public class Main {    public static void main(String[] args) {        AnnotationConfigApplicationContext context =                new AnnotationConfigApplicationContext(ElConfig.class);        ElConfig resourceService = context.getBean(ElConfig.class);        resourceService.outputResource();        context.close();    }}

运行结果

这里写图片描述