Spring Boot实战(二)Spring常用配置 2.2 Spring EL和资源调用

2.2.1 点睛
Spring EL-Spring表达语言,支持在xml和注解中使用表达式,类似于JSP的EL表达式语言。
Spring开发中经常涉及调用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等,我们可以使用Spring的表达式语言实现资源的注入。
Spring主要在注解@Value的参数中使用表达式。
本节演示实现以下几种情况:
(1)注入普通字符;
(2)注入操作系统属性;
(3)注入表达式运算结果;
(4)注入其他Bean的属性;
(5)注入文件内容;
(6)注入网址内容;
(7)注入属性文件。
2.2.2示例
(1)准备,增加commons-io可简化文件相关操作,本例中使用commons-io将file转换成字符串:

    <dependency>
    	<groupId>commons-io</groupId>
    	<artifactId>commons-io</artifactId>
    	<version>2.3</version>
    </dependency>

在com.wisely.highlight_spring4.ch2.el 包下新建test.txt,内容随意。
在com.wisely.highlight_spring4.ch2.el包下新建test.properties,内容如下:

book.author=admin
book.name=spring boot

(2)需被注入的Bean。

package com.wisely.highlight_spring4.ch2.el;

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

public class DemoService {
	
	@Value("其他类的属性") //此处为注入普通字符串
	private String another;

	public String getAnother() {
		return another;
	}

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

}

(3)演示配置类。

package com.wisely.highlight_spring4.ch2.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;

@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch2.el")
@PropertySource("classpath:com/wisely/highlight_spring4/ch2/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.author") //注入其它Bean属性
	private String fromAnother;
	
	@Value("classpath:com/wisely/highlight_spring4/ch2/el/test.txt") //注入文件资源
	private Resource testFile;
	
	@Value("${book.name}") //注入配置配件需使用@PropertySource指定文件地址,若使用@Value注入,则要配置一个PropertySourcesPlaceholderConfigurer的Bean。注意,@Value("${book.name}")使用的是$而不是“#”
	private String bookName;
	
	@Value("http://www.baidu.com") //注入网址资源
	private Resource testUrl;  
	
	@Autowired
	private Environment environment; //注入properties还可从Environment中获得。
	
	@Bean //将PropertySourcesPlaceholderConfigurer注入到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()));
			System.out.println(IOUtils.toString(testUrl.getInputStream()));
			System.out.println(bookName);
			System.out.println(environment.getProperty("book.author"));
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

}

(4)运行。

package com.wisely.highlight_spring4.ch2.el;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

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();
	}
}

结果如图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值