4. Spring4.x EL&资源调用

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


    commons-io
    commons-io
    2.5

  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文件 内容随意
4. Spring4.x EL&资源调用_第1张图片
  1. 在resouce目录下创建一个test.properties文件
p.name=zhangsan
p.age=23
4. Spring4.x EL&资源调用_第2张图片
  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内容


 
......百度一下,你就知道......

zhangsan
23

你可能感兴趣的:(4. Spring4.x EL&资源调用)