ITPub博客

首页 > 应用开发 > IT综合 > Spring EL

Spring EL

原创 IT综合 作者:百联达 时间:2017-02-16 17:01:08 0 删除 编辑
一:说明

Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似于JSP的EL表达式语言。

Spring 开发中经常涉及调用各种资源的情况,包含普通文件,网址,配置文件,系统环境变量等。 我们可以使用Spring的表达式语言实现资源的注入。

二:代码实例


点击(此处)折叠或打开

  1. @Service
  2. public class DemoService {
  3.     @Value("其他类的属性")
  4.     private String another;

  5.     /**
  6.      * @return the another
  7.      */
  8.     public String getAnother() {
  9.         return another;
  10.     }

  11.     /**
  12.      * @param another
  13.      * the another to set
  14.      */
  15.     public void setAnother(String another) {
  16.         this.another = another;
  17.     }

  18. }


点击(此处)折叠或打开

  1. @Configuration
  2. @ComponentScan("com.gemdale")
  3. @PropertySource("classpath:com/gemdale/gmap/spring/boot/demo/el/test.properties")
  4. public class ElConfig {
  5.     @Value("I love youe!")
  6.     private String normal;

  7.     @Value("#{systemProperties['os.name']}")
  8.     private String osName;

  9.     @Value("#{ T(java.lang.Math).random() * 100.0 }")
  10.     private double randomNumber;

  11.     @Value("#{demoService.another}")
  12.     private String fromAnother;

  13.     @Value("classpath:com/gemdale/gmap/spring/boot/demo/el/test.txt")
  14.     private Resource testFile;

  15.     @Value("http://www.baidu.com")
  16.     private Resource testUrl;

  17.     @Value("${book.name}")
  18.     private String bookName;

  19.     @Autowired
  20.     private Environment environment;

  21.     @Bean
  22.     public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
  23.         return new PropertySourcesPlaceholderConfigurer();
  24.     }

  25.     public void outputResource() {
  26.         try {
  27.             System.out.println(normal);
  28.             System.out.println(osName);
  29.             System.out.println(randomNumber);
  30.             System.out.println(fromAnother);
  31.             System.out.println(IOUtils.toString(testFile.getInputStream()));
  32.             System.out.println(IOUtils.toString(testUrl.getInputStream()));
  33.             System.out.println(bookName);
  34.             System.out.println(environment.getProperty("book.author"));
  35.         }
  36.         catch (Exception e) {
  37.            e.printStackTrace();
  38.         }
  39.     }
  40. }

点击(此处)折叠或打开

  1. public class Start {
  2.     public static void main(String[] args) throws Exception {

  3.         AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(
  4.                 ElConfig.class);
  5.         ElConfig resourceService = configApplicationContext.getBean(ElConfig.class);
  6.         resourceService.outputResource();

  7.         configApplicationContext.close();
  8.     }
  9. }



点击(此处)折叠或打开

  1. book.author=GH
  2. book.name=spring boot


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28624388/viewspace-2133687/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: MongoVUE 连接失败
请登录后发表评论 登录
全部评论
15年以上互联网经验,先后从事过制造业,证券业,物业行业和物流行业信息系统和互联网产品的设计落地实施,8年系统架构经验。最近关注企业数字化转型、财务数字化服务共享、大语言模型、Kubernetes微服务架构和Istio微服务治理框架。

注册时间:2013-02-05

  • 博文量
    332
  • 访问量
    1265640