1、编写配置文件

  1. #债权转让
  2. #默认周期 必须大于0
  3. credit.defaultDuration=1
  4. #最小转让金额(元)
  5. credit.minBidAmount=1.00
  6. #最小转让时间 到期时间小于此的不让进行转让(小时)
  7. credit.assignThreshold=24
  8. #最小折让率(%)
  9. credit.minDiscountRate=70
  10. #最大折让率(%)
  11. credit.maxDiscountRate=110
2、配置 spring 
  1. <!-- 配置参数 -->
  2. <bean id="configProperties"
  3. class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  4. <property name="locations">
  5. <value>file:${config.root}/admin-config.properties</value>
  6. </property>
  7. </bean>
  8. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
  9. <property name="properties" ref="configProperties" />
  10. </bean>
3、编写 Java类
可以 注入 静态属性 ,方法 要非静态
  1. package com.netfinworks.fax.admin.web.config;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.stereotype.Component;
  6. import com.netfinworks.common.util.money.Money;
  7. /**
  8. * <p>
  9. * 常量配置
  10. *</p>
  11. * @author weichunhe
  12. * @version $Id: Constant.java, v 0.1 2015年5月28日 下午5:14:22 weichunhe Exp $
  13. */
  14. @Component
  15. public class Constant {
  16. private Logger log = LoggerFactory.getLogger(getClass());
  17. private static final String LOG_PREFIX = "从配置文件中注入常量==>";
  18. /**
  19. * 债权转让 默认周期 周期必须大于0
  20. */
  21. private static int defaultDuration=1;
  22. @Value("#{configProperties['credit.defaultDuration']}")
  23. public void setDefaultDuration(String defaultDuration) {
  24. try {
  25. Constant.defaultDuration = Integer.valueOf(defaultDuration);
  26. } catch (Exception e) {
  27. log.error(LOG_PREFIX+"注入默认周期出错!"+defaultDuration,e);
  28. }
  29. }
  30. /**
  31. * 债权转让 最小转让时间 到期时间小于此的不让进行转让(毫秒)
  32. */
  33. private static long assignThreshold = 24*60*60*1000;
  34. @Value("#{configProperties['credit.assignThreshold']}")
  35. public void setAssignThreshold(String assignThreshold) {
  36. try {
  37. Constant.assignThreshold = Long.valueOf(assignThreshold) * 60 * 60 *1000L;
  38. } catch (Exception e) {
  39. log.error(LOG_PREFIX+"注入 最小转让时间出错!"+assignThreshold,e);
  40. }
  41. }
  42. /**
  43. * 债权转让 最小转让金额(元)
  44. */
  45. private static Money minBidAmount ;
  46. @Value("#{configProperties['credit.minBidAmount']}")
  47. public void setMinBidAmount(Money minBidAmount) {
  48. Constant.minBidAmount = minBidAmount;
  49. }
  50. /**
  51. * 债权转让最小折让率(%)
  52. */
  53. private static int minDiscountRate = 0;
  54. @Value("#{configProperties['credit.minDiscountRate']}")
  55. public void setMinDiscountRate(String minDiscountRate) {
  56. try {
  57. Constant.minDiscountRate =Integer.valueOf(minDiscountRate);
  58. } catch (Exception e) {
  59. log.error(LOG_PREFIX+"注入最小折让率出错!"+minDiscountRate,e);
  60. }
  61. }
  62. /**
  63. * 债权转让最大折让率(%)
  64. */
  65. private static int maxDiscountRate = 110;
  66. @Value("#{configProperties['credit.maxDiscountRate']}")
  67. public void setMaxDiscountRate(String maxDiscountRate) {
  68. try {
  69. Constant.maxDiscountRate =Integer.valueOf(maxDiscountRate);
  70. } catch (Exception e) {
  71. log.error(LOG_PREFIX+"注入最大折让率出错!"+maxDiscountRate,e);
  72. }
  73. }
  74. public static int getMinDiscountRate() {
  75. return minDiscountRate;
  76. }
  77. public static int getMaxDiscountRate() {
  78. return maxDiscountRate;
  79. }
  80. public static long getAssignThreshold() {
  81. return assignThreshold;
  82. }
  83. public static Money getMinBidAmount() {
  84. return minBidAmount;
  85. }
  86. public static int getDefaultDuration() {
  87. return defaultDuration;
  88. }
  89. }
4、使用 
  1. model.put("defaultDuration", Constant.getDefaultDuration());
  2. model.put("minDiscountRate", Constant.getMinDiscountRate());
  3. model.put("maxDiscountRate", Constant.getMaxDiscountRate());

spring 将配置文件中的值注入 属性的更多相关文章

  1. 将springboot配置文件中的值注入到静态变量

    SpringBoot配置文件分为.properties和.yml两种格式,根据启动环境的不同获取不同环境的的值. spring中不支持直接注入静态变量值,利用spring的set注入方法注入静态变量 ...

  2. springboot属性类自动加载配置文件中的值

    springboot属性类自动加载配置文件中的值,如Person类加载在yml中配置的name,age等属性值,可以通过如下步骤获取: 类上添加@ConfigurationProperties注解,p ...

  3. struts2配置文件中action的name属性

    struts2配置文件中action的name属性的第一个字符不要加斜杠 <action name="see" class="baoxiuManage_seeAct ...

  4. ThinkPHP 获取配置文件中的值

    C('SPECIAL_USER'):获取配置文件中的值 存入数组

  5. spring读取classpath目录下的配置文件通过表达式去注入属性值.txt

    spring读取配置文件: 1. spring加载配置文件: <context:property-placeholder location="classpath:config/syst ...

  6. 配置文件中取值: spring配置文件中util:properties和context:property-placeholder

    转载大神 https://blog.csdn.net/n447194252/article/details/77498916 util:properties和context:property-plac ...

  7. spring 3配置文件中如何注入map list set等类型

    首先写个 javabean类吧,如下 package com.bean; import java.util.List; import java.util.Map; import java.util.P ...

  8. 日志配置文件读取spring boot配置文件中的属性

    如果是读取 application.properties 这种spring boot的默认配置文件时 其中 scope固定为context  指明从上下文中获取, name 根据自己的意思给, sou ...

  9. Spring根据XML配置文件 p名称空间注入属性(property后出现,简便但只针对基本数据类型管用,自定义集合等引用类型无效)

    要生成对象并通过名称空间注入属性的类 代码如下: package com.swift; public class User { private String userName; public void ...

随机推荐

  1. JSOI最大值 (线段树)

    change 单点修改 query 区间最值 Program XJOI2321; ; ..maxn*] of longint; i,m,n,ans,p,x:longint; ch:char; func ...

  2. Cocoa -- 添加和移除开机启动项

    一 写plist到~/Library/LaunchAgents/ 目录下 // 配置开机默认启动 -(void)installDaemon{ NSString* launchFolder = [NSS ...

  3. kendo中需要kendo.default.min.css

    kendo中需要kendo.default.min.css,这个默认是没有加入头文件的,还是需要手动加入一下 <link href="~/Scripts/kendo/styles/ke ...

  4. PHP小白学习日程之旅

    我是一名专升本的学生,在这里偶然接触了博客园,我觉得非常好,每天可以在这里看别人的分享与学习,还会在大学学习俩年,我只想专注的吧自己的技术提高,跟园子里的朋友们一起学习与分享加油!!!!!!!!!!! ...

  5. HTML5:表格

    表格的作用是显示二维数据.在HTML5中不再同意用表格控制页面内容的布局.而是採用新增的CSS表格特性(这里不涉及CSS,将在后面介绍). 以下主要介绍用于制作表格的HTML元素. 构建表格 表格的基 ...

  6. 苹果iPhone连接wifi就进去www.apple.com主页 下边显示 &lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Success&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY&gt;Success&lt;/BODY&gt;

    苹果iPhone连接wifi就进去www.apple.com主页  下边显示 <HTML><HEAD><TITLE>Success</TITLE>< ...

  7. J2EE肌肉系统—四层模型

    J2EE是基于JAVA技术的一种标准.为什么会有这种标准呢? 主要是在企业级应用开发其中有一些需求.比如数据库连接,邮件服务.事件处理等,都是一些通用模块. 而这些模块假设由开发者来开发.势必添加开发 ...

  8. UVA 11748 - Rigging Elections(dfs)

    UVA 11748 - Rigging Elections option=com_onlinejudge&Itemid=8&page=show_problem&category ...

  9. Xamarin nuget package update 错误

    update xamarin.Forms包时出现错误: 'The specified path, file name, or both are too long. The fully qualifie ...

  10. svg 虚线

    用 svg 元素画出一条直线很简单,用 line 等元素都可以,可是画虚线的话,就需要一个属性: stroke-dasharray: 1 2;  画1px 空2px (长这个样子) stroke-da ...