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