在Spring中使用SpringEL进行依赖注入的简单实例

为了演示方便,本文所有文件均放在ch2.el包目录下。有错误之处或者其他问题的读者请留言。

1、话不多说,直接上代码

创建DemoService.java文件

package ch2.el;

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

@Service
public class DemoService {
	@Value("其他类的属性")
	private String another;
	
	public String getAnother(){
		return another;
	}
	
	public void setAnother(String another){
		this.another = another;
	}
}

创建ElConfig.java文件

package ch2.el;

import java.io.InputStreamReader;

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("ch2.el")
@PropertySource("classpath:ch2/el/test.properties")	//7、
public class ElConfig {
	@Value("I Love You!")	//1、注入普通字符串
	private String normal;	
	
	@Value("#{systemProperties['os.name']}")	//2、注入操作系统属性
	private String osName;
	
	@Value("#{ T(java.lang.Math).random() * 100.0 }")	//3、注入表达式结果
	private double randomNumber;
	
	@Value("#{demoService.another}")	//4、注入其他bean的属性
	private String fromAnother;
	
	@Value("classpath:ch2/el/test.txt")	//5、注入文件资源
	private Resource testFile;
	
	@Value("https://www.baidu.com/")	//6、注入网址资源
	private Resource testUrl;
	
	@Value("${book.name}")	//7、注入配置文件
	private String bookName;
	
	@Autowired //7
	private Environment environment;
	
	@Bean	//7
	public static  PropertySourcesPlaceholderConfigurer placeholderConfigurer(){
		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(new InputStreamReader(testFile.getInputStream())));

			System.out.println(bookName);
			System.out.println(environment.getProperty("book.author"));
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

创建Main.java文件

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

创建test.properties文件

book.author=qifengle
book.name=spring boot

创建test.txt文件

--------test.txt-------------
Hello Spring Boot
-----------------------------

2、运行Main.java,获得输出结果如下:

I Love You!
Windows 10
98.5512836922682
其他类的属性
--------test.txt-------------
Hello Spring Boot
-----------------------------
spring boot
qifengle

3、SpringEL还有其他注入用法:

下面的不演示结果了,直接贴用法示例,都很简单:

@Component
public class OperatingBean {
	//数字和字符串都可以用"=="或"eq"进行相等比较
	@Value("#{role.id == 1}")
	private boolean equalNum;
	
	@Value("#{role.note eq 'note_1'}")//eq :判断是否相等。
	private boolean equalStr;
	
	@Value("#{role.id > 2}")
	private boolean greater;

	@Value("#{role.id>1?5:-5}")
	private int max;
	
	@Value("#{role.note?:'hello'}")//若note值为空,则给它赋值为hello
	private String defaultStr ;
    
//**setter 和 getter方法**//

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值