Spring4.x經常使用配置(二):Spring EL和資源調用

 

 

 

 

 

 

 

一. 點睛

Spring EL-Spring表達式語言,支持在xml和註解中使用表達式,相似於JSPEL表達式語言。java

Spring開發中常常涉及調用各類資源的狀況,包含普通文件,網址,配置文件,系統環境變量等,咱們能夠使用Spring的表達式語言實現資源的注入。git

Spring主要在註解@Value的參數中使用表達式。github

下面咱們將會演示如下幾種狀況:web

(1) 注入普通字符
(2) 注入操做系統屬性
(3) 注入表達式運算結果
(4) 注入其餘Bean的屬性
(5) 注入文件內容
(6) 注入網址內容
(7) 注入屬性文件spring

二. 示例

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,內容以下:apache

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

代碼解釋:

①此外爲注入普通字符less

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的屬性
⑤注入文件內容
⑥注入網址內容
⑦注入屬性文件dom

注入配置文件須要使用@PropertySource指定文件地址,若使用@Value注入,則要配置一個PropertySourcesPlaceholderConfigurer的Bean。
注意:@Value("${book.name}")使用的是」$」,而不是」#」。
注入Properties還能夠從Environment中得到this

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

運行結果以下圖所示:
elspa

5. 源代碼示例:

github地址:點擊查看
碼雲地址:點擊查看

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

相關文章
相關標籤/搜索