美文网首页
4. Spring4.x EL&资源调用

4. Spring4.x EL&资源调用

作者: 第八号灬当铺 | 来源:发表于2017-08-29 11:53 被阅读0次

    首先在 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
    

    相关文章

      网友评论

          本文标题:4. Spring4.x EL&资源调用

          本文链接:https://www.haomeiwen.com/subject/kacodxtx.html