在项目中,有些参数需要配置到属性文件xxx.properties中,这样做是为了维护方便,如果需要变动只需修改属性文件,不需要重新编译项目就可以了,非常方便。

而为了使用起来方便,可以通过将属性值注入到类的静态字段中(static),这样就可以用className.fieldName的方式来获取了。

1.servlet-context.xml

 <!-- spring的属性加载器,加载properties文件中的属性 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config.properties" />
</bean>
<context:component-scan base-package="com.jykj.demo.util" />

注意: 这里需要配置spring自动扫描的包名,该包下包含了需要被注解的类ConfigInfo

2. config.properties (示例属性)

admin_id=1
default_password=888888

3.ConfigInfo (对应的配置bean)

package com.jykj.demo.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class ConfigInfo { public static int admin_id;
public static String default_password; //属性配置文件
@Value("${admin_id}")
public void setAdmin_id(int admin_id) {
ConfigInfo.admin_id = admin_id;
}
@Value("${default_password}")
public void setDefault_password(String default_password) {
ConfigInfo.default_password = default_password;
}
}

注意: 这里需要将自动生成setter的方法的修饰符static去掉,否则spring无法注入

4. 使用

在任何类中直接使用 ConfigInfo.xxx 即可方便引用,如 ConfigInfo.default_password

这个虽然简单,但我花了很久的时间从网上找寻各种解决方案,所以有必要写下来,这样可以方便以后尽快找到答案不要浪费时间。

 
 

Spring MVC 属性文件读取注入到静态字段的更多相关文章

  1. spring mvc:属性无法自动注入

    在使用spring mvc 3开发一个项目模块时,遇到这样一个奇怪的问题: 前端页面发送的请求中,所有参数都无法自动注入到指定的@ModelAttribute对象中,经过检查,参数名称与接受对象的属性 ...

  2. Spring MVC的文件上传和下载

    简介: Spring MVC为文件上传提供了直接的支持,这种支持使用即插即用的MultipartResolver实现的.Spring MVC 使用Apache Commons FileUpload技术 ...

  3. Spring MVC 实现文件的上传和下载

    前些天一位江苏经贸的学弟跟我留言问了我这样一个问题:“用什么技术来实现一般网页上文件的上传和下载?是框架还是Java中的IO流”.我回复他说:“使用Spring MVC框架可以做到这一点,因为Spri ...

  4. 0062 Spring MVC的文件上传与下载--MultipartFile--ResponseEntity

    文件上传功能在网页中见的太多了,比如上传照片作为头像.上传Excel文档导入数据等 先写个上传文件的html <!DOCTYPE html> <html> <head&g ...

  5. Spring MVC-学习笔记(5)spring MVC的文件上传、下载、拦截器

    1.文件上传.      spring MVC为文件上传提供了直接的支持,这种支持是即插即用的MultipartResolver(多部分解析器)实现的.spring MVC使用Apache Commo ...

  6. Spring MVC实现文件上传

    基础准备: Spring MVC为文件上传提供了直接支持,这种支持来自于MultipartResolver.Spring使用Jakarta Commons FileUpload技术实现了一个Multi ...

  7. Spring MVC的文件上传

    1.文件上传 文件上传是项目开发中常用的功能.为了能上传文件,必须将表单的method设置为POST,并将enctype设置为multipart/form-data.只有在这种情况下,浏览器才会把用户 ...

  8. Spring的属性文件properties使用注意

    Spring的属性文件properties使用注意 Spring 中属性文件的配置 通常我们会使用properties文件来设置一些属性,如数据库连接信息,避免进行硬编码, <bean clas ...

  9. spring mvc: 属性方法名称解析器(多动作控制器)MultiActionController/ControllerClassNameHandlerMapping/PropertiesMethodNameResolver

    spring mvc: 属性方法名称解析器(多动作控制器) 加入控制器是StudentContrller.java,里面有3个方法 index,add,remove 那么访问地址是: http://l ...

随机推荐

  1. java日期的运用(DateUtils工具类)

    public static void main(String[] args) { Date now = new Date(); SimpleDateFormat sd = new SimpleDate ...

  2. Rhel6-lvs配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 相关网址:http://zh.linuxvirtualserver.org/ yum仓库配置: [rh ...

  3. Program A-归并排序

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  4. Problem A 栈

    Description   You are given a string consisting of parentheses () and []. A string of this type is s ...

  5. SrcollView分页加载数据(第二种方法 自定义listView)

    package com.baidu.ms; import android.content.Context;import android.util.AttributeSet;import android ...

  6. Android获取手机设备识别码(IMEI)和手机号码

    最近看了下获取手机设备ID和手机信息以及SIM的信息例子,主要还是借鉴别人的,现在自己写一下,算是巩固加深了,也希望能给大家一个参考 必要的条件还是一部真机,SIM卡或者UIM卡. 首先,在Andro ...

  7. 苹果推送APNS自己总结

    开发状态服务器地址 gateway.sandbox.push.apple.com 2195 产品状态服务器地址 gateway.push.apple.com         2195 Developm ...

  8. julia生成指定格式的字符串.jl

    julia生成指定格式的字符串.jl """ julia生成指定格式的字符串.jl http://bbs.bathome.net/thread-39829-1-1.htm ...

  9. IOS文件管理-NSFileMangager-NSdata

    Ios下的文件管理, Ios下不像windows 文件系统那样可以访问任何的文件目录,如C盘.D盘什么的.在Ios中每个应用程序只能访问当前程序的目录,也即sandbox(沙盒模型). iOS为每个应 ...

  10. python 绘图工具 matplotlib 入门

    转自: http://www.cnblogs.com/kaituorensheng/p/3440273.html matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的 ...