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

相关文章
相关标签/搜索