Spring常用配置 - EL及使用资源文件

一、Spring EL是什么?

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

二、怎么使用?

Spring主要在 @Value注解 使用表达式, 实现资源的注入。
可以注入包括以下内容:

  • 普通字符
  • 操作系统属性
  • 表达式运算结果
  • 其他Bean的属性
  • 文件内容
  • 网址内容
  • 属性文件
三、ELDemo

一个憨憨的Bean

package com.cactus.demo.el;

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

/**
 * Created by liruigao
 * Date: 2019-10-11 20:52
 * Description:
 * 通过@Value注入内容
 */

@Component
public class Demo {
    @Value("乱七八糟其他的")
    public String another;

    public String getAnother() {
        return another;
    }

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

一个傻傻的Config文件

package com.cactus.demo.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;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by liruigao
 * Date: 2019-10-11 20:56
 * Description:
 */

@Configuration
@ComponentScan("com.cactus.demo.el")
// 注意配置文件和文本文件要放到resources目录下,才可以获取到该文件
@PropertySource("classpath:eldemo/test.properties")
public class ELConfig {

    @Value("hi 647")
    private String normal;

    @Value("#{systemProperties['os.name']}")
    private String osName;

    // bean名字若未命名,则默认首字母小写
    @Value("#{demo.another}")
    private String demoAnother;

    @Value("classpath:eldemo/test.txt")
    private Resource testFile;

    @Value("http://www.baidu.com")
    private Resource testUrl;

    @Value("${book.name}")
    private String bookName;

    // 配置文件的数据同样可以通过Environment获取
    @Autowired
    private Environment environment;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    public void output() {
        try {
            System.out.println(normal);
            System.out.println(osName);
            System.out.println(demoAnother);
            System.out.println(IOUtils.toString(testFile.getInputStream()));
            System.out.println(IOUtils.toString(testUrl.getInputStream()));
            System.out.println(bookName);
            //通过environment获取配置文件数据
            System.out.println("env : " + environment.getProperty("book.author"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Main

package com.cactus.demo.el;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by liruigao
 * Date: 2019-10-12 10:29
 * Description:
 */


public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ELConfig.class);
        ELConfig elConfig = context.getBean(ELConfig.class);
        elConfig.output();
        context.close();
    }
}

result
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜鹊先生Richard

随缘~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值