Spring4.x常用配置(二):Spring EL和资源调用

 

 

 

 

 

 

 

一. 点睛

Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似于JSPEL表达式语言。

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

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

下面我们将会演示以下几种情况:

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

二. 示例

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

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.3</version>
</dependency>

org.light4j.sping4.usually.el包下新建test.txt,内容随意。
org.light4j.sping4.usually.el包下新建test.properties,内容如下:

book.author=longjiazuo
book.name=spring boot

2. 编写需被注入的Bean

package org.light4j.sping4.usually.el;

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

@Service
public class DemoService {
    @Value("其他类的属性") //①
    private String another;

    public String getAnother() {
        return another;
    }

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

代码解释:

①此外为注入普通字符

3.演示配置类

package org.light4j.sping4.usually.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;

@Configuration
@ComponentScan("org.light4j.sping4.usually.el")
@PropertySource("classpath:org/light4j/sping4/usually/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}") //④
    private String fromAnother;

    @Value("classpath:org/light4j/sping4/usually/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 //7
    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();
        }
    }
}

代码解释

①注入普通字符
②注入操作系统属性
③注入表达式运算结果
④注入其他Bean的属性
⑤注入文件内容
⑥注入网址内容
⑦注入属性文件

注入配置文件需要使用@PropertySource指定文件地址,若使用@Value注入,则要配置一个PropertySourcesPlaceholderConfigurer的Bean。
注意:@Value("${book.name}")使用的是”$”,而不是”#”。
注入Properties还可以从Environment中获得

4. 运行

package org.light4j.sping4.usually.el;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

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();
    }

}

运行结果如下图所示:
el

5. 源代码示例:

github地址:点击查看
码云地址:点击查看

文章摘自 https://juejin.im/entry/59e7fd696fb9a045146323f1

原文地址:https://www.cnblogs.com/love-cj/p/8349880.html