实现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文件的更多相关文章

  1. 谈谈Spring 注入properties文件总结

    本篇谈谈Spring 注入properties文件总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 spring提供了多种方式来注入properties文件,本文做一个 ...

  2. spring boot 在框架中注入properties文件里的值(Spring三)

    前一篇博客实现了打开第一个页面 链接:https://blog.csdn.net/qq_38175040/article/details/105709758 本篇博客实现在框架中注入propertie ...

  3. 【Spring MVC】Properties文件的加载

    [Spring MVC]Properties文件的加载 转载:https://www.cnblogs.com/yangchongxing/p/10726885.html 参考:https://java ...

  4. Spring加载properties文件的两种方式

    在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...

  5. Spring如何引用properties文件里的…

    [zt]Spring如何引用properties文件里的配置 来自 http://blog.csdn.net/luobo525/archive/2006/11/06/1370258.aspx 1.Pr ...

  6. spring自动注入的三种方式

    所谓spring自动注入,是指容器中的一个组件中需要用到另一个组件(例如聚合关系)时,依靠spring容器创建对象,而不是手动创建,主要有三种方式: 1. @Autowired注解——由spring提 ...

  7. Spring 通过配置文件注入 properties文件

    当我们需要将某些值放入 properties文件 key=value 的方式,获取文件信息使用spring 注入的方式会变得很便捷 1. spring 配置文件需要导入 <?xml versio ...

  8. Spring 中使用Properties文件

    Spring提供了加载Properties文件的工具类:org.springframework.beans.factory.config.PropertyPlaceholderConfigurer. ...

  9. Spring加载properties文件的属性的值

    要使用配置文件的值首先在spring.xml配置加载properties文件 <context:property-placeholder location="classpath:ife ...

随机推荐

  1. 学习Javascript闭包(Closure)

    闭包作用 1.让变量驻留在内存中 2.函数外部可以读取函数内部的私有变量 <!DOCTYPE html> <html lang="en"> <head ...

  2. 使用CSS把ul,li制作成表格

    查看效果:http://hovertree.com/texiao/css/7.htm 具体实现请看样式部分. 完整代码: <!DOCTYPE html> <html> < ...

  3. [deviceone开发]-一个简单的表单示例

    一.简介 这个例子我们用do_ScrollView+do_LinearLayout来实现,当没有输入相关的值而去保存的时候,则把隐藏的几个提示Label显示出来,然后调用do_LinearLayout ...

  4. 那些过目不忘的H5页面

    原文链接:http://isux.tencent.com/great-mobile-h5-pages.html 从引爆朋友圈的H5小游戏<围住神经猫>,到颠覆传统广告的大众点评H5专题页& ...

  5. HashMap和SparseArray的性能比较。

    HashMap和SparseArray可以实现相似的功能. 但SparseArray是Android定义的,在键是整数时,他比HashMap的性能更高,因为HashMap使用的是Integer对象, ...

  6. 在xib中用KVC修改控件属性

    比如我们想在xib 中设置按钮的圆角,这样的话我们就可以在xib文件中进行修改,具体操作如下 实现效果,如下

  7. iOS开发之功能模块--高仿Boss直聘的IM界面交互功能

    本人公司项目属于社交类,高仿Boss直聘早期的版本,现在Boss直聘界面风格,交互风格都不如Boss直聘以前版本的好看. 本人通过iPhone模拟器和本人真机对聊,将完成的交互功能通过Mac截屏模拟器 ...

  8. fillStyle径向渐变

    <!DOCTYPE HTML> <head> <meta charset = "utf-8"> <title>canvas</ ...

  9. Html--表单练习

          <!--文档定义一定要带上,因为浏览器在解析的时候先按照文档定义的格式解析,   如果没有就按照浏览器默认的格式解析,可能会出问题.-->   <html>   & ...

  10. Linux命令学习总结:last

    命令简介:     该命令用来列出目前与过去登录系统的用户相关信息.指令英文原义:show listing of last logged in users 执行权限 :有些需要特殊权限 指令所在路径: ...