Spring EL和资源调用
点睛
Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似于JSP的EL表达式语言。
Spring来发中经常涉及调用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等,外面可以使用Spring的表达式语言实现资源注入。
Spring主要在注解@Value的参数中使用表达式。
本节演示以下几种情况:
注入普通字符;
注入操作系统属性;
注入表达式运算结果;
注入其他Bean属性;
注入文件内容;
注入网址内容;
注入属性文件。
示例
准备,增加commons-io可简化文件相关操作,本例中使用commons-io将file转换成字符串:
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency> |
在hightlight_spirng4.ch2.el.包下新建test.txt,内容随意。
在hightlight_spirng4.ch2.el.包下新建test.properties,内容如下:
book.author=wangyunfei book.name=spring boot |
需被注入的Bean
package hightlight_spirng4.ch2.el;
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service;
@Service public class DemoService { @Value("other property") // 注入普通字符串 public String another;
public String getAnother() { return another; }
public void setAnother(String another) { this.another = another; }
}
|
演示配置类
package hightlight_spirng4.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("hightlight_spirng4.ch2.el") @PropertySource("classpath:hightlight_spirng4/ch2/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:hightlight_spirng4/ch2/el/test.txt") // 注入文件资源 private Resource testFile;
@Value("https://xuim2-web.x-network.jp/process/user_see.html") // 注入网址资源 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(bookName); System.out.println(environment.getProperty("book.author")); System.out.println(IOUtils.toString(testUrl.getInputStream(), "utf-8")); } catch (Exception e) { e.printStackTrace(); }
}
}
|
代码解释
注入配置配件需使用@PropertySource指定文件地址,若使用@Value注入,则需要配置一个PropertySourcePlaceholderConfigurer的Bean。注意,@Value(“${book.name}”)使用的是$,而不是#。
运行
package hightlight_spirng4.ch2.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(); } } |
结果
10 03, 2017 2:25:28 午後 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh情報: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1da6444: startup date [Tue Oct 03 14:25:28 JST 2017]; root of context hierarchy I LOVE YOU Windows 7 1.5335814231934308 other property this is test file! spring boot wangyunfei <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html dir="ltr"> <head>…… |
转载于:https://blog.51cto.com/11603441/2122307