properties.config的读取

PropertiesUtil.java

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : ServiceInitListener.java
//  @ Date : 2013/1/16
//  @ Author : czp
//	@ Title : 配置文件加载工具
//  @ Description :  对config.properties进行读取,由ServiceInitListener进行初始化
//
//

package com.fjxhx.business.system.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

public class PropertiesUtil {

	// 只做读取,如添加修改须加入线程安全
	private static Logger logger = Logger.getLogger(PropertiesUtil.class);
	private static Properties configProperty;
	private static Properties errorProperties;

	public static void createProperties(String path) {
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(path));
			configProperty = new Properties();
			configProperty.load(in);
		} catch (IOException e) {
			logger.error(e);
		}
	}

	public static void createErrorProperties(String path) {
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(path));
			errorProperties = new Properties();
			errorProperties.load(in);
		} catch (IOException e) {
			logger.error(e);
		}
	}

	// 中文必须要要用(\u6211\u662F\u7F16\u7801)编码
	public static String getConfigProperty(String key) {
		String value = getConfigProperty().getProperty(key);
		if (value != null) {
			return value;
		}
		return null;
	}

	public static String getErrorProperty(String key) {
		String value = getErrorProperties().getProperty(key);
		if (value != null) {
			return value;
		}
		return null;
	}

	public static Properties getErrorProperties() {
		return errorProperties;
	}

	public static void setErrorProperties(Properties errorProperties) {
		PropertiesUtil.errorProperties = errorProperties;
	}

	public static Properties getConfigProperty() {
		return configProperty;
	}

	public static String objectReplace(String value, Object object) {
		if (value == null) {
			return null;
		}
		String regex = "\\$([^\\$]+)\\$";// 匹配所有 $hello$ 这类标记
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(value);
		while (m.find()) {
			String pre = m.group(1);
			String getFun = "get" + pre.substring(0, 1).toUpperCase()
					+ pre.substring(1);
			Class klass = object.getClass();
			try {
				Method method = klass.getMethod(getFun);
				Object obj = method.invoke(object);
				if (obj == null) {
					obj = "";
				}
				if (Date.class.isAssignableFrom(obj.getClass())) {
					obj = PropertiesUtil.parseDate((Date) obj);
				}
				value = value.replaceAll("\\$" + pre + "\\$", obj.toString());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return value;
	}

	private static String dateFormat = "yyyy-MM-dd HH:mm:ss";

	public static Properties toProperties(Object bean) {
		if (bean == null)
			return null;
		Properties pro = new Properties();
		Class klass = bean.getClass();
		Method[] methods = klass.getMethods();
		Method method;
		String name, key;
		Object obj = null;
		for (int i = 0; i < methods.length; i += 1) {
			try {
				method = methods[i];
				name = method.getName();
				key = "";
				if (name.startsWith("get")) {
					key = name.substring(3);
				} else if (name.startsWith("is")) {
					key = name.substring(2);
				}
				if (key.length() > 0 && !key.equals("Class")
						&& Character.isUpperCase(key.charAt(0))
						&& method.getParameterTypes().length == 0) {
					key = key.substring(0, 1).toLowerCase() + key.substring(1);
					obj = method.invoke(bean, (Object[]) null);
					// 仅处理 String, Number, Boolean, Date 这几种类型
					if (obj == null) {

					} else if (Date.class.isAssignableFrom(obj.getClass())) {
						pro.setProperty(key, parseDate((Date) obj, dateFormat));
					} else if (obj instanceof Object) {
						pro.setProperty(key, obj.toString());
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return pro;
	}

	/** 日期到字符串 */
	public static String parseDate(Date date, String pattern) {
		if (date == null) {
			return null;
		}
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			return sdf.format(date);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public static String parseDate(Date date) {
		if (date == null) {
			return null;
		}
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
			return sdf.format(date);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
}

 读取:

private final static String ORDER_SUC = PropertiesUtil.getConfigProperty().getProperty("weixin.order.templateId");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 中,你可以使用 `@ConfigurationProperties` 注解来读取 application.properties 文件中的 JSON 配置。 首先,确保你的 application.properties 文件中包含了 JSON 格式的配置。例如: ```properties my.config={"key1":"value1", "key2":"value2"} ``` 然后,在你的配置类中,添加 `@ConfigurationProperties` 注解并指定前缀。例如: ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "my") public class MyConfig { private Map<String, String> config; public Map<String, String> getConfig() { return config; } public void setConfig(Map<String, String> config) { this.config = config; } } ``` 现在,你可以在其他组件或服务中注入 `MyConfig` 对象,并访问其中的配置信息。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MyService { private final MyConfig myConfig; @Autowired public MyService(MyConfig myConfig) { this.myConfig = myConfig; } public void printConfig() { System.out.println(myConfig.getConfig().get("key1")); // 输出 "value1" System.out.println(myConfig.getConfig().get("key2")); // 输出 "value2" } } ``` 这样,你就可以在 `MyService` 中通过 `myConfig` 对象获取到从 application.properties 文件中读取的 JSON 配置信息了。注意,要确保正确引入必要的依赖,并在 Spring Boot 应用启动时扫描到配置类和组件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值