为了演示方便,本文所有文件均放在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方法**//
}