【Spring 4.x】Spring常用配置_Spring EL和资源调用

概念

Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似于JSP的EL表达式语言。

Spring开发中经常涉及调用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等,我们可以使用Spring的表达式语言实现资源的注入

Spring主要在注解@Value的参数中使用表达式。

(1)注入普通字符;
(2)注入操作系统属性;
(3)注入表达式运算结果;
(4)注入其他Bean的属性;
(5)注入文件内容;
(6)注入网址内容;
(7)注入属性文件。

示例

(1)准备,增加commons-io可简化文件相关操作,本例中使用commons-io将file转换成字符串:

<dependency> 
    <groupId>commons-io</groupId> 
    <artifactId>commons-io</artifactId> 
    <version>2.3</version> 
</dependency>

在com.wisely.highlight_spring4.ch2.el包下新建test.txt,内容随意。

在com.wisely.highlight_spring4.ch2.el包下新建test.properties,内容如下:

 book.author=wangyunfei
 book.name=spring boot

(2)需被注入的Bean。

package com.wisely.highlight_spring4.ch2.el;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class DemoService {
    @Value("其他类的属性") //1 注入普通字符串
    private String another;
    public String getAnother() {
        return another;
    }
    public void setAnother(String another) {
        this.another = another;
    }
}

①此处为注入普通字符串

(3)演示配置类。

package com.wisely.highlight_spring4.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("com.wisely.highlight_spring4.ch2.el")
@PropertySource("classpath:com/wisely/highlight_spring4/ch2/el/test.properties")//7
public class ElConfig {
    @Value("I Love You!") //1 注入普通字符串
    private String normal;
    @Value("#{systemProperties['os.name']}") //2 注入操作系统属性
    private String osName;
    @Value("#{ T(java.lang.Math).random() * 100.0 }") //3 注入表达式结果
    private double randomNumber;
    @Value("#{demoService.another}") //4 注入其他Bean属性
    private String fromAnother;
    @Value("classpath:com/wisely/highlight_spring4/ch2/el/test.txt") //5 注入文件资源
    private Resource testFile;
    @Value("http://www.baidu.com") //6 注入网址资源
    private Resource testUrl;
    @Value("${book.name}") //7 注入配置文件
    private String bookName;
    @Autowired
    private Environment environment; //7 注入配置文件
    @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();
        }
    }
}

注入配置配件需使用@PropertySource指定文件地址,若使用@Value注入,则要配置一个PropertySourcesPlaceholderConfigurer的Bean。注意,@Value(" b o o k . n a m e " ) 使 用 的 是 “ {book.name}")使用的是“ book.name"使”,而不是“#”。

注入Properties还可从Environment中获得。

(4)运行

package com.wisely.highlight_spring4.ch2.el;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
    public static void main(String[] args) {
         AnnotationConfigApplicationContext context =
                    new AnnotationConfigApplicationContext(ResourceConfig.class);
         ElConfig resourceService = context.getBean(ElConfig.class);
         resourceService.outputResource();
         context.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值