Spring Configuration(三):Spring EL——Spring的表达式语言

Spring EL——Spring表达式语言:
    支持在xml和注解中使用表达式,类似于JSP的EL表达式语言。
    Spring开发中经常涉及调用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等,我们可能使用Spring的表达式语言实现资源的注入。
    Spring主要在@Value的参数中使用表达式。
    示例:
        注入普通字符串:
            @Value("你好啊!I love you!")
            private String normal;


        注入操作系统属性:            
            @Value("#{systemProperties['os.name']}")
            private String osName;


        注入表达式运算结果: 
            @Value("#{ T(java.lang.Math).random() * 100.0 }")
            private double random Number


        注入其他Bean的属性
            @Value("#{demoService.another}")
            private String fromAnother


        注入文件内容
            @Value("classpath:com/wisely/highlight_spring4/ch2/el/test.txt")
            private Resource testFile


        注入网址
            @Value("http://www.baidu.com")
            private Resource testUrl


        注入属性文件:用@PropertySource("classpath:com/wisely/../test.properties")指定属性文件
            @Value("${book.name}")
             private String bookName

猜你喜欢

转载自blog.csdn.net/lsxf_xin/article/details/80045022