【Spring】Spring常用配置-Spring EL和资源调用
发布日期:2021-06-29 13:38:39
浏览次数:2
分类:技术文章
本文共 4268 字,大约阅读时间需要 14 分钟。
转载请注明出处:
本文源自【】
分析
先简单介绍下Spring EL。
Spring EL 也就是Spring表达式语言,支持在xml和注解中使用表达式,类似于JSP的EL表达式语言。Spring开发中我们可能经常涉及到调用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等,我们可以使用Spring的表达式语言实现资源的注入。
Spring主要在注解@Value的参数中使用表达式。
本示例演示实现以下几种情况:
1、注入普通的字符串 2、注入操作系统属性 3、注入表达式运算结果 4、注入其他Bean的属性 5、注入文件内容 6、注入网址内容 7、注入属性文件在本节演示中,我遇到一个问题,已在此博客中解决,如有朋友遇到,请参考本篇博客解决:
进行本示例的演示,需要先配置好Maven和Spring哦、
见:示例
因为需要将file转换成字符串,我们增加commons-io可以简化文件的相关操作、
在pom文件中增加如下代码:commons-io commons-io 2.4
然后,在当前类的目录下新建test.txt。内容随意。
我的内容如下:测试文件内容:Spring
然后再新建test.properties文件,内容如下,当然,你也可以自己修改:
project.name=SpringELproject.author=chenhaoxiang
写需要被注入的Bean:
package cn.hncu.p2_2_2SpringEL;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Service;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/11/13. * Time: 下午 9:06. * Explain:被注入的Bean */@Servicepublic class DemoService { @Value("DemoService类的属性")//注入字符串 private String another; public String getAnother() { return another; } public void setAnother(String another) { this.another = another; }}
增加配置类:
package cn.hncu.p2_2_2SpringEL;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;import java.io.IOException;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/11/13. * Time: 下午 9:11. * Explain:配置类 */@Configuration@ComponentScan("cn.hncu.p2_2_2SpringEL")@PropertySource("classpath:cn/hncu/p2_2_2SpringEL/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("${project.name}")//注入配置文件 private String projectName; @Value("classpath:cn/hncu/p2_2_2SpringEL/test.txt") private Resource testFile;//注意这个Resource是:org.springframework.core.io.Resource; @Autowired //注入配置文件 private Environment environment; @Value("http://www.chaojijuhui.com")//注入网址资源 private Resource testUrl; @Bean //注入配置文件 public static PropertySourcesPlaceholderConfigurer propertyConfigurer(){ return new PropertySourcesPlaceholderConfigurer(); } public void outputResource(){ try { System.out.println("normal:"+normal); System.out.println("osName:"+osName); System.out.println("randomNumber:"+randomNumber); System.out.println("fromAnother:"+fromAnother); System.out.println("projectName:"+projectName); System.out.println("测试文件:"+IOUtils.toString(testFile.getInputStream())); System.out.println("配置文件project.author:"+environment.getProperty("project.author")); System.out.println("网址资源:"+IOUtils.toString(testUrl.getInputStream())); } catch (IOException e) { e.printStackTrace(); } }}
注入配置配件需要使用@PropertySource指定文件地址,若使用@Value注入,则要配置一个PropertySourcesPlaceholderConfigurer的Bean。
注意,@Value(“${project.name}”)使用的是”$
“而不是”#”。 上面的类演示了这2中配置配件的方式! 运行类:
package cn.hncu.p2_2_2SpringEL;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/11/13. * Time: 下午 11:44. * Explain:运行类 */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();}}
运行结果:
项目链接—具体包:
本文章由编写, 所有权利保留。
转载请注明出处:
本文源自【】
转载地址:https://chenhx.blog.csdn.net/article/details/53156288 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月15日 10时02分06秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Shopify!Shopify!Shopify!
2019-04-29
这是美国MarTech最大的一家独立公司:HubSpot
2019-04-29
从开发到产出:关于机器学习的七则干货建议
2019-04-29
你想成为数据科学家吗?不要把机器学习当成入门第一课
2019-04-29
你想成为数据科学家吗?不要把机器学习当成入门第一课
2019-04-29
现代社会悖论:信息泛滥是一只不守规矩的野兽
2019-04-29
如何设计自己的第一个加密交易机器人?
2019-04-29
浪费在Excel上的时间:如何开始专家式机器学习实验追踪?
2019-04-29
失业三星期:我寻找第二份编程工作之路
2019-04-29
跳过媒介,我们能不能只用思想控制计算机?
2019-04-29
服务器宕机:谷歌最近经历了“黑客攻击”吗?
2019-04-29
RepVGG:极简架构,SOTA性能,让VGG式模型再次伟大
2019-04-29
提升查询技能,这7条SQL查询错误必须解决
2019-04-29
TKDE 2020 | 综述:基于知识图谱的推荐系统
2019-04-29
休息时间!哪些业余活动能提升开发人员的技能?
2019-04-29
把人脑“森林”上传到计算机:人类脑计划只是一个科幻野心?
2019-04-29
智力测试:如何超越动物之智?
2019-04-29
事关人类生存?为什么要探寻AI系统的可解释性?
2019-04-29
安全工程师必知:常见Java漏洞有哪些?
2019-04-29
数据科学中的计量经济学技术
2019-04-29