Spring(十四):SpringEL表达式与@Value注解调用资源

star2017 1年前 ⋅ 359 阅读

Spring项目,常会把一些公共参数或可能修改的参数放到资源文件中,在代码中引入资源文件中参数,如:url,密钥,环境变量等。

Spring提供了@Value注解来支持资源的引入。

注入资源包括

  1. 注入普通字符串
  2. 注入操作系统属性(系统环境)
  3. 注入表达式结果
  4. 注入其经它Bean属性
  5. 注入文件资源
  6. 注入网址资源
  7. 注入配置文件

注入资源示例

  1. 创建一个类,添加属性并注入字符。

     import org.springframework.beans.factory.annotation.Value;
     import org.springframework.stereotype.Service;
    
     @Service
     public class DemoServiceImpl {
    
         @Value("其它类的属性")//注入普通字符
         private String another;
    
         public String getAnother() {
             return another;
         }
    
         public void setAnother(String another) {
             this.another = another;
         }
     }
    
  2. 创建注入演示类(配置类),加载资源文件

     import java.io.IOException;
    
     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.spring.el")
     @PropertySource("classpath:resources.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("#{demoServiceImpl.another}")
         private String fromAnother;//其它Bean属性
    
         @Value("classpath:test.txt")
         private Resource testFile;
    
         @Value("http://www.baidu.com")
         private String testUrl;
    
         @Value("${book.name}")
         private String bookName;
    
         @Autowired
         private Environment environment;
    
         @Bean
         public static PropertySourcesPlaceholderConfigurer propertyConfig() {
             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(), "UTF-8"));
                 System.out.println(testUrl);
                 System.out.println(new String(bookName.getBytes("iso-8859-1"), "utf-8"));
                 System.out.println(environment.getProperty("book.author"));
                 System.out.println();
             } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
         }
    
     }
    
  3. 创建主类业调用配置类

     import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
     public class ELMain {
    
         public static void main(String[] args) {
             AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ElConfig.class);
    
             ElConfig resourceService = context.getBean(ElConfig.class);
             resourceService.outputResource();
    
             context.close();
         }
     }
    
  4. 创建资源文件resources.properties,内容如下:

     book.author=Rocky
     book.name=Spring Boot实战
    
  5. 创建普通文件test.txt,内容如下:

     Hello,中华人民
    
  6. 输出结果如下:

     I Love You
     Windows 10
     77.00793826116237
     其它类的属性
     Hello,中华人民
     http://www.baidu.com
     Spring Boot实战
     Rocky
    
更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: