spring El

Posted JohnEricCheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring El相关的知识,希望对你有一定的参考价值。

 1 package com.wisely.heighlight_spring4.ch2.el;
 2 
 3 import java.io.IOException;
 4 
 5 import org.apache.commons.io.IOUtils;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.beans.factory.annotation.Value;
 8 import org.springframework.context.annotation.Bean;
 9 import org.springframework.context.annotation.ComponentScan;
10 import org.springframework.context.annotation.Configuration;
11 import org.springframework.context.annotation.PropertySource;
12 import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
13 import org.springframework.core.env.Environment;
14 import org.springframework.core.io.Resource;
15 
16 @Configuration
17 @ComponentScan("com.wisely.heighlight_spring4.ch2.el")
18 @PropertySource("classpath:com/wisely/heighlight_spring4/ch2/el/test.properties")    //加载属性资源文件
19 public class ElConfig {
20     @Value("I love you!")    //普通的字符串
21     private String normal;
22     
23     @Value("#{systemProperties[\'os.name\']}")    //系统属性
24     private String osName;
25     
26     @Value("#{ T(java.lang.Math).random() * 100.0 }")        //表达式结果
27     private double randomNumber;
28     
29     @Value("#{demoService.another}")    //其他Bean属性
30     private String fromAnother;
31     
32     @Value("classpath:com/wisely/heighlight_spring4/ch2/el/test.txt")    //文件资源
33     private Resource testFile;
34     
35     @Value("http://www.baidu.com")    //网址资源
36     private Resource testUrl;
37     
38     @Value("${book.name}")        //配置文件
39     private String bookName;
40     
41     @Autowired
42     private Environment environment;
43     
44     @Bean
45     public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
46         return new PropertySourcesPlaceholderConfigurer();
47     }
48     
49     public void outputResource() {
50         try {
51             System.out.println("normal:"+normal);
52             System.out.println("osName:"+osName);
53             System.out.println("randomNumber:"+randomNumber);
54             System.out.println("fromAnother:"+fromAnother);
55             
56             System.out.println("testFile:"+IOUtils.toString(testFile.getInputStream()));
57             System.out.println("testUrl:"+IOUtils.toString(testUrl.getInputStream()));
58             System.out.println("bookName:"+bookName);
59             System.out.println("environment:"+environment.getProperty("book.author"));
60         } catch (IOException e) {
61             e.printStackTrace();
62         }
63     }
64 }
 1 package com.wisely.heighlight_spring4.ch2.el;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.stereotype.Service;
 5 
 6 @Service
 7 public class DemoService {
 8     @Value("其它类的属性")
 9     private String another;            //注入普通的字符串
10 
11     public String getAnother() {
12         return another;
13     }
14 
15     public void setAnother(String another) {
16         this.another = another;
17     }
18 
19 }

 

以上是关于spring El的主要内容,如果未能解决你的问题,请参考以下文章

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

Spring boot:thymeleaf 没有正确渲染片段

What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

Spring Rest 文档。片段生成时 UTF-8 中间字节无效 [重复]