Spring EL-Spring
表达式语言,支持在xml
和注解中使用表达式,相似于JSP
的EL
表达式语言。java
Spring
开发中常常涉及调用各类资源的状况,包含普通文件,网址,配置文件,系统环境变量等,咱们能够使用Spring
的表达式语言实现资源的注入。git
Spring
主要在注解@Value
的参数中使用表达式。github
下面咱们将会演示如下几种状况:web
(1) 注入普通字符
(2) 注入操做系统属性
(3) 注入表达式运算结果
(4) 注入其余Bean的属性
(5) 注入文件内容
(6) 注入网址内容
(7) 注入属性文件spring
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
,内容以下:apache
book.author=longjiazuo book.name=spring boot
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; } }
①此外为注入普通字符less
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的属性
⑤注入文件内容
⑥注入网址内容
⑦注入属性文件dom
注入配置文件须要使用@PropertySource
指定文件地址,若使用@Value
注入,则要配置一个PropertySourcesPlaceholderConfigurer
的Bean。
注意:@Value("${book.name}")
使用的是”$”,而不是”#”。
注入Properties
还能够从Environment
中得到this
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(); } }
运行结果以下图所示:spa
文章摘自 https://juejin.im/entry/59e7fd696fb9a045146323f1