4. Spring4.x EL&资源调用

首先在 pom.xml中引入commons-io 用于将流转换成字符串

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>
  1. 新建一个属性类
package com.xiaohan.el;

import org.springframework.stereotype.Service;

// 演示的属性类
@Service
public class DemoService {
    private String another = "类的属性";
    public String getAnother() {
        return another;
    }
}
  1. 在resouce目录下创建一个test.txt文件 内容随意
  1. 在resouce目录下创建一个test.properties文件
p.name=zhangsan
p.age=23
  1. 新建一个使用资源的类
package com.xiaohan.el;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;


@Service
public class UseResourceService {

    //普通字符串
    @Value("Hello")
    private String normal;

    // 操作系统属性
    @Value("#{systemProperties['os.name']}")
    private String osName;

    // 表达式结果
    @Value("#{T(java.lang.Math).random() * 100}")
    private double randomNumber;

    // 其它Bean的属性
    @Value("#{demoService.another}")
    private String fromAnother;

    // 文件资源
    @Value("classpath:test.txt")
    private Resource testFile;

    // 网络资源
    @Value("http://www.baidu.com")
    private Resource baiduHtml;

    // 配置文件
    @Value("${p.name}")
    private String author;

    // 配置文件
    @Autowired
    private Environment environment;

    public void outputResource() {
        try {
            System.err.println(normal);
            System.err.println(osName);
            System.err.println(randomNumber);
            System.err.println(fromAnother);
            System.err.println(IOUtils.toString(testFile.getInputStream()));
            System.err.println(IOUtils.toString(baiduHtml.getInputStream()));
            System.err.println(author);
            System.err.println(environment.getProperty("p.age"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  1. 配置类
package com.xiaohan.el;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ComponentScan("com.xiaohan.el")

// 加载资源文件
@PropertySource("classpath:test.properties")
public class ElConfig {
}
  1. Main测试类
package com.xiaohan.el;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

// EL&资源调用
public class Main {
    public static void main(String[] args) {
        ApplicationContext ac = new AnnotationConfigApplicationContext(ElConfig.class);
        UseResourceService useResourceService = ac.getBean(UseResourceService.class);
        useResourceService.outputResource();
    }
}

输出

Hello
Windows 10
5.099909206748354
类的属性
test.txt内容

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css>
......<title>百度一下,你就知道</title>......

zhangsan
23
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,075评论 6 342
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,473评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,955评论 18 399
  • 一. Java基础部分.................................................
    wy_sure阅读 9,255评论 0 11
  • 这个话题首先从我自己的理财经历说起。 我不炒股,只会买买基金。 05年读第一个硕士的时候,学校每个月会发给公费生几...
    鼎睿阅读 3,068评论 2 2