Spring EL和资源调用

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


Spring主要在注解@Value的参数中使用表达式:

(1)注入普通字符

(2)注入操作系统属性

(3)注入表达式运算结果

(4)注入其他Bean的属性

(5)注入文件内容

(6)注入网址内容

(7)注入属性文件


示例代码如下:

1、准备,增加commons-io可简化文件相关操作,本例中使用commons-io将file转换成字符串

 <!--通过commons-io来简化文件相关操作-->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.3</version>
        </dependency>

2、在com.lwh.highlight_spring4.ch2.el包下新建test.properties,内容如下:

book.author=luwenhu
book.name=spring boot

3、需要被注入的bean

package com.lwh.highlight_spring4.ch2.el;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * Created by luwenhu on 2017/9/19.
 */
@Service
public class DemoService {
    //在这里注入普通字符串
    @Value("其他类的属性")
    private String another;
    public String getAnother() {
        return another;
    }

    public void setAnother(String another) {
        this.another = another;
    }


}

4、演示配置类

package com.lwh.highlight_spring4.ch2.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;


/**
 * Created by luwenhu on 2017/9/19.
 */
@Configuration
@ComponentScan("com.lwh.highlight_spring4.ch2.el")
//指定属性文件的全路径
//classpath:其实就是打包之后的classes下
@PropertySource("classpath:com/lwh/highlight_spring4/ch2/el/test.properties")
public class ElConfig {
    @Value("zju")//(1)注入普通字符串
    private String normal;

    @Value("#{systemProperties['os.name']}") //(2)注入操作系统属性,注意格式
    private String osName;

    @Value("#{T(java.lang.Math).random()*100.0}")//(3)注入表达式结果
    private double randomNumber;

    @Value("#{demoService.another}")//(4)注入其他bean的属性
    private String fromAnother;

    @Value("classpath:com/lwh/highlight_spring4/ch2/el/test.txt")//(5)注入文件资源
    private Resource testFile;

    @Value("http://www.baidu.com")//(6)注入网站资源
    private Resource testUrl;

    @Value("${book.name}")//(7)注入配置文件,注意使用的是$而不是#
    private String bookName;

    /**
     * 关于@Autowired和@Bean
     * @Autowired:可注解在set方法和属性上,用来注入bean,习惯上注解在属性上,它是属于注解配置
     * @Bean:注解在方法上,声明当前方法的返回值为一个Bean,它是属于java配置
     */
    @Autowired
    private Environment environment;//注入Properties还可以从Environment中获得

    //注入配置配件需要使用@PropertySoure指定文件地址,若使用@Value注入,则要配置一个
    //PropertySourcesPlaceholderConfigurer的bean

    @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()));
            System.out.println(IOUtils.toString(testUrl.getInputStream()));

            System.out.println(bookName);
            System.out.println(environment.getProperty("book.author"));

        }catch (Exception e){
            e.printStackTrace();
        }
    }

}

5、运行

package com.lwh.highlight_spring4.ch2.el;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by luwenhu on 2017/9/19.
 */
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();
    }
}

6、运行结果展示

注意:

如果你的程序run之后报找不到com.lwh.highlight_spring4.ch2.el下面的file的原因是,你的D:highlight_spring4 argetclassescomlwhhighlight_spring4ch2el目录下面没有test.properties或者test.txt文件。因为classpath是指编译后classes下的路径!!!!!

原文地址:https://www.cnblogs.com/wenhulu/p/7559737.html