05-SpringBoot——Spring常用配置-Spring EL和资源调用
来源:互联网 发布:淘宝网天猫阿兴家艾条 编辑:程序博客网 时间:2024/06/13 02:23
Spring常用配置-Spring EL和资源调用
【博文目录>>>】
【项目源码>>>】
【Spring EL和资源调用】
Spring EL-Spring 表达式语言,支持在xml和注解中使用表达式,类似于JSP 的EL 表达式语言。Spring 开发中经常涉及谓用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等,我们可以使用Spring 的表达式语言实现资源的注入。
Spring 主要在注解@Value 的参数中使用表达式,可以实现但不限于以下几种情况:
(1) 注入普通字符:(2) 注入操作系统属性:(3) 注入表达式运算结果;(4) 注入其他Bean 的属性:(5) 注入文件内容:(6) 注入网址内容:(7) 注入属性文件。
【代码实现】
package com.example.spring.framework.el;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Service;/** * Author: 王俊超 * Date: 2017-07-10 22:47 * All Rights Reserved !!! */@Servicepublic class DemoService { @Value("其他类的属性") // 注入普通字符串 private String another; public String getAnother() { return another; } public void setAnother(String another) { this.another = another; }}
package com.example.spring.framework.el;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;/** * Author: 王俊超 * Date: 2017-07-10 22:47 * All Rights Reserved !!! */@Configuration@ComponentScan("com.example.spring.framework.el")@PropertySource("classpath:com/example/spring/framework/el/test.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("#{demoService.another}") // 注入其他Bean属性 private String fromAnother; @Value("classpath:com/example/spring/framework/el/test.txt") // 注入文件资源 private Resource testFile; @Value("http://www.baidu.com") // 注入网址资源 private Resource testUrl; @Value("${book.name}") // 注入配置文件 private String bookName; @Autowired private Environment environment; @Bean public static PropertySourcesPlaceholderConfigurer propertyConfigure() { 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(IOUtils.toString(testUrl.getInputStream(), "utf-8")); System.out.println(bookName); System.out.println(environment.getProperty("book.author")); } catch (Exception e) { e.printStackTrace(); } }}
package com.example.spring.framework.el;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Author: 王俊超 * Date: 2017-07-10 22:42 * All Rights Reserved !!! */public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ElConfig.class); ElConfig resourceService = context.getBean(ElConfig.class); resourceService.outputResource(); context.close(); }}
运行结果
阅读全文
1 0
- 05-SpringBoot——Spring常用配置-Spring EL和资源调用
- 【Spring】Spring常用配置-Spring EL和资源调用
- 04-SpringBoot——Spring常用配置-Bean的Scope
- 07-SpringBoot——Spring常用配置-Profiles
- 08-SpringBoot——Spring常用配置-事件
- 12-SpringBoot——Spring MVC基础-常用配置
- 06-SpringBoot——Spring常用配置-Bean的初始化和销毁
- 聊聊Spring4.x常用配置(二):EL表达式和资源调用
- Spring配置EL表达式
- SpringBoot:spring boot 服务配置和部署
- 11-SpringBoot——Spring MVC基础-常用注解
- spring boot初探(二)——springboot配置log4j
- 02-SpringBoot——Spring基础-Java配置
- SpringBoot——自动配置依赖[spring-boot-starter-XXX]
- SpringBoot学习-第二章 常用配置方式-<Spring Boot 实战>
- spring和springboot
- 【SpringBoot】Spring-->SpringBoot 习惯优于配置
- spring和springboot 多数据源配置和问题解决
- csuoj 1970 LXX数
- 阶乘因式分解(一)
- C++学习-函数
- 6174问题
- [SpringMVC] SpringMVC拦截器
- 05-SpringBoot——Spring常用配置-Spring EL和资源调用
- 谁获得了最高奖学金
- 【正一专栏】向伟大的人民解放军致敬
- csuoj1971 安排座位
- nyoj_笨小熊——不走传统
- ios-绘图样式
- JavaScript(四)
- 小学生算术
- Storm应用中遇到的问题与解决方案