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 }

 

posted @ 2018-03-21 17:37  JohnEricCheng  阅读(225)  评论(0编辑  收藏  举报