概念
Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似于JSP的EL表达式语言。
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>
在com.wisely.highlight_spring4.ch2.el包下新建test.txt,内容随意。
在com.wisely.highlight_spring4.ch2.el包下新建test.properties,内容如下:
book.author=wangyunfei
book.name=spring boot
(2)需被注入的Bean。
package com.wisely.highlight_spring4.ch2.el;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
@Value("其他类的属性") //1 注入普通字符串
private String another;
public String getAnother() {
return another;
}
public void setAnother(String another) {
this.another = another;
}
}
①此处为注入普通字符串
(3)演示配置类。
package com.wisely.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;
@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch2.el")
@PropertySource("classpath:com/wisely/highlight_spring4/ch2/el/test.properties")//7
public class ElConfig {
@Value("I Love You!") //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/wisely/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
private Environment environment; //7 注入配置文件
@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();
}
}
}
注入配置配件需使用@PropertySource指定文件地址,若使用@Value注入,则要配置一个PropertySourcesPlaceholderConfigurer的Bean。注意,@Value(" b o o k . n a m e " ) 使 用 的 是 “ {book.name}")使用的是“ book.name")使用的是“”,而不是“#”。
注入Properties还可从Environment中获得。
(4)运行
package com.wisely.highlight_spring4.ch2.el;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ResourceConfig.class);
ElConfig resourceService = context.getBean(ElConfig.class);
resourceService.outputResource();
context.close();
}
}