Java类获取配置文件中的静态常量
通过注解方式获取常量值
当我们在编写代码过程中,部分常量信息可能需要多处公用,通常会把这些常量写进xml配置文件中,通过相关配置信息在代码中使用。如下dingding:的相关常量的值;
@Value注解进行配置文件信息的获取
新建一个常量获取类DingDingConstant,声明各种常量,并写入set方法,在set方法上使用@Value注解获取配置文件中的值,并赋值给相应的对象,括号中通过 @Value("${dingding.agentId}") 可指定读取配置文件中的对象。
package com.mallplus.common.constant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author: SUN
* @version: 1.0
* @date: 2020/10/9 19:33
* @description: 项目中的常量定义类
*/
@Component
public class DingDingConstant {
/**
* 企业应用标识(ISV调用必须设置)
*/
public static Long AgentId;
/**
*
*/
public static String AppKey;
/**
*
*/
public static String AppSecret;
/**
* 企业corpid, 需要修改成开发者所在企业
*/
public static String CorpId;
/**
* 加解密需要用到的token,ISV(服务提供商)推荐使用注册套件时填写的token,普通企业可以随机填写
*/
public static String Token;
/**
* 数据加密密钥。用于回调数据的加密,长度固定为43个字符,从a-z, A-Z, 0-9共62个字符中选取,
* 您可以随机生成,ISV(服务提供商)推荐使用注册套件时填写的EncodingAESKey
*/
public static String AesKey;
/**
* 接收事件回调的url,必须是公网可以访问的url地址
*/
public static String Url; // 本地测试回调地址
/**
* 审批表单中详情拼接URL地址
*/
public static String DetailUrl;
@Value("${dingding.agentId}")
public void setAgentId(Long agentId) {
AgentId = agentId;
System.out.println("- - - agentId- " + agentId);
}
@Value("${dingding.appKey}")
public void setAppKey(String appKey) {
AppKey = appKey;
System.out.println("- - - appKey- " + appKey);
}
@Value("${dingding.appSecret}")
public void setAppSecret(String appSecret) {
AppSecret = appSecret;
System.out.println("- - - appSecret- " + appSecret);
}
@Value("${dingding.corpId}")
public void setCorpId(String corpId) {
CorpId = corpId;
System.out.println("- - - corpId- " + corpId);
}
@Value("${dingding.token}")
public void setToken(String token) {
Token = token;
System.out.println("- - - token- " + token);
}
@Value("${dingding.aesKey}")
public void setAesKey(String aesKey) {
AesKey = aesKey;
System.out.println("- - - aesKey- " + aesKey);
}
@Value("${dingding.url}")
public void setUrl(String url) {
Url = url;
System.out.println("- - - url- " + url);
}
@Value("${dingding.detailUrl}")
public void setDetailUrl(String detailUrl) {
DetailUrl = detailUrl;
System.out.println("- - - detailUrl- " + detailUrl);
}
}
@ConfigurationProperties注解获取配置文件
@ConfigurationProperties 相比**@Value** 注解进行配置文件信息的读取配置起来较为简单;只需在类名上添加注解,并指定对应的配置信息即可,并添加get() set() 方法;
package com.mallplus.common.utils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
/**
* @author: SUN
* @version: 1.0
* @date: 2020/10/28 9:44
* @description:
*/
@ConfigurationProperties(prefix = "dingding")
@Component
public class AliyunLinkUtils {
// public 即可外部引用,通过类名直接引用,如:AliyunLinkUtils.agentId;
// 若设置为 private 即只能在此类内部使用;
public static String agentId;
public static String appKey;
public static String appSecret;
public String getAgentId() {
return agentId;
}
public void setAgentId(String agentId) {
System.out.println("-- agentId- -1 - " + agentId);
this.agentId = agentId;
}
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
public String getAppSecret() {
return appSecret;
}
public void setAppSecret(String appSecret) {
this.appSecret = appSecret;
}
}