Spring自动注入properties文件
实现spring 自动注入属性文件中的key-value。
1、在applicationContext.xml配置文件中,引入<util />命名空间。
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
2、配置注解组件扫描,用注解来自动注入
<context:component-scan base-package="com.packagename" ></context:component-scan>
3、在classpath路径下创建属性文件,如sys.properties
test=sysdata
4、 让Spring载入属性文件,在applicationContext.xml 中配置
<util:properties id="sys" location="classpath:sys.properties"/>
5、创建java文件,让Spring注入从资源文件中读取到的属性的值,如下
@Component
public class SysConf { @Value("#{sys.test}")
private String test; @Value("#{sys.test}")
public void setTest(String test){
test = test;
} @Value("#{sys}")
public void setSysConf(Properties sys){
test= sys.getProperty("test");
}
}
注意:这里的#{sys} 是与第四步的id=sys 相对应的
Spring自动注入properties文件的更多相关文章
- 谈谈Spring 注入properties文件总结
本篇谈谈Spring 注入properties文件总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 spring提供了多种方式来注入properties文件,本文做一个 ...
- spring boot 在框架中注入properties文件里的值(Spring三)
前一篇博客实现了打开第一个页面 链接:https://blog.csdn.net/qq_38175040/article/details/105709758 本篇博客实现在框架中注入propertie ...
- 【Spring MVC】Properties文件的加载
[Spring MVC]Properties文件的加载 转载:https://www.cnblogs.com/yangchongxing/p/10726885.html 参考:https://java ...
- Spring加载properties文件的两种方式
在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...
- Spring如何引用properties文件里的…
[zt]Spring如何引用properties文件里的配置 来自 http://blog.csdn.net/luobo525/archive/2006/11/06/1370258.aspx 1.Pr ...
- spring自动注入的三种方式
所谓spring自动注入,是指容器中的一个组件中需要用到另一个组件(例如聚合关系)时,依靠spring容器创建对象,而不是手动创建,主要有三种方式: 1. @Autowired注解——由spring提 ...
- Spring 通过配置文件注入 properties文件
当我们需要将某些值放入 properties文件 key=value 的方式,获取文件信息使用spring 注入的方式会变得很便捷 1. spring 配置文件需要导入 <?xml versio ...
- Spring 中使用Properties文件
Spring提供了加载Properties文件的工具类:org.springframework.beans.factory.config.PropertyPlaceholderConfigurer. ...
- Spring加载properties文件的属性的值
要使用配置文件的值首先在spring.xml配置加载properties文件 <context:property-placeholder location="classpath:ife ...
随机推荐
- Code First :使用Entity. Framework编程(7) ----转发 收藏
第7章 高级概念 The Code First modeling functionality that you have seen so far should be enough to get you ...
- Web(Jsp+ Servlet)开发中如何解决中文乱码问题
1.中文乱码的成因 编码的字符集和解码的字符集不一致. 2.web开发过程中可能出现的乱码的位置及解决方案 ①request乱码 在向服务器传递数据时,所传递的中文有可能出现乱码. post请求(协议 ...
- js 理解new的运行机制
先上段代码: function People(name) { this.name = name; } People.prototype.sayName = function () { console. ...
- $(function) ready onload 等区别
新手接触javascript.jquery的时候不可避免的要接触题目所标识的相关内容,反复看过几次一到用的时候总是不踏实,写文以记之. 符号“$”是jquery对象(个人这样理解,拥有函数的用法).接 ...
- 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(十三)台风模块
config.xml文件的配置如下: <widget label="台风" icon="assets/images/typhoon.png" config ...
- SAP中需要记住的一些标准表
E070 请求号的抬头表 E071 请求号的行项目表 E07T 请求号的文本 E71K TASK的抬头(子请求) ******************************************* ...
- 如何将github上的 lib fork之后通过podfile 改变更新源到自己fork的地址
解决办法: http://stackoverflow.com/questions/20936885/cocoapods-and-github-forks 就是fork完后,提交更改到自己的github ...
- iOS--KVO的概述与使用
一.概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应 ...
- TeamCity实战(1):准备工作
环境: Windows Server 2008 R2 SP1(不会再有SP2,参考这里:http://www.techspot.com/news/50599-microsoft-wont-releas ...
- this的作用--转载
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...