05-SpringBoot——Spring常用配置-Spring EL和资源调用

Spring常用配置-Spring EL和资源调用


【博文目录>>>】


【项目源码>>>】


【Spring EL和资源调用】


Spring EL-Spring 表达式语言,支持在xml和注解中使用表达式,类似于JSP 的EL 表达式语言。Spring 开发中经常涉及谓用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等,我们可以使用Spring 的表达式语言实现资源的注入。

Spring 主要在注解@Value 的参数中使用表达式,可以实现但不限于以下几种情况:

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

【代码实现】


package com.example.spring.framework.el;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * Author: 王俊超
 * Date: 2017-07-10 22:47
 * All Rights Reserved !!!
 */
@Service
public class DemoService {
    @Value("其他类的属性") // 注入普通字符串
    private String another;

    public String getAnother() {
        return another;
    }

    public void setAnother(String another) {
        this.another = another;
    }

}
package com.example.spring.framework.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;

/**
 * Author: 王俊超
 * Date: 2017-07-10 22:47
 * All Rights Reserved !!!
 */
@Configuration
@ComponentScan("com.example.spring.framework.el")
@PropertySource("classpath:com/example/spring/framework/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}") // 注入其他Bean属性
    private String fromAnother;

    @Value("classpath:com/example/spring/framework/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
    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(), "utf-8"));
            System.out.println(IOUtils.toString(testUrl.getInputStream(), "utf-8"));
            System.out.println(bookName);
            System.out.println(environment.getProperty("book.author"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
package com.example.spring.framework.el;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Author: 王俊超
 * Date: 2017-07-10 22:42
 * All Rights Reserved !!!
 */
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();
    }
}

运行结果

这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值