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;
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值