Spring EL怎么使用

2023-04-20,

这篇文章主要讲解了“Spring EL怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring EL怎么使用”吧!

一:说明

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

感谢各位的阅读,以上就是“Spring EL怎么使用”的内容了,经过本文的学习后,相信大家对Spring EL怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是本站,小编将为大家推送更多相关知识点的文章,欢迎关注!

《Spring EL怎么使用.doc》

下载本文的Word格式文档,以方便收藏与打印。