单列模式-数据加载实例

所谓单例,就是整个程序有且仅有一个实例。该类负责创建自己的对象,同时确保只有一个对象被创建。在Java,一般常用在工具类的实现或创建对象需要消耗资源。

特点

  • 类构造器私有
  • 持有自己类型的属性
  • 对外提供获取实例的静态方法

 以下为数据加载实例:

package com.xx.utils;

import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.xx.common.Constant;
import com.xx.pojo.basic.BsXXAuthEntity;
import com.xx.service.basic.BsXXAuthService;
/**
 *  Class Name: XXAuthConfigScond.java
 *  Description: 
 *  DateTime 2019年8月30日 下午12:23:29 
 *  @version 1.0
 *  
 */
@Component
public class XXAuthUtils {

	public static Logger logger = LoggerFactory.getLogger(XXAuthUtils.class);

	@Reference(group = "basic", interfaceClass = BsXXAuthService.class, check = false, timeout = 10000, retries = 0)
	private static BsXXAuthService bsXXAuthService;

	private static XXAuthConfig XXAuthConfig = null;
	
	@Autowired
	private RedisUtils redisUtils;

	/**
	 * 重新加载
	 */
	public  void reloadXXAuthConfig() {
		
		synchronized (XXAuthUtils.class) {
			try {
				String config = redisUtils.get(Constant.CACHE_DEFAULT_XX_AUTH_CONFIG);
				logger.info("XXAuthConfig init by redis. config: " + JSON.toJSONString(config));
				if (ObjectUtils.isEmpty(XXAuthConfig)  || StringUtils.isEmpty(config)) {
					loadCurrent(config);
				}else {
					BsXXAuthEntity bsXXAuth =JSON.parseObject(config, BsXXAuthEntity.class);
					if(!XXAuthConfig.getUserName().equals(bsXXAuth.getUserName())) {//当pin不一致时,强制刷新
						logger.info("XXAuthConfig init  redis. refresh...  ");
						loadCurrent(null);
					}
				}
				logger.info("XXAuthConfig init success. config:" + JSON.toJSONString(XXAuthConfig));
			} catch (Exception e) {
				// TODO: handle exception
				logger.error(String.format("XXAuthConfig init failed: %s", e));
			}
			
		}
	}

	/**
	 * 加载当前默认证参数
	 */
	private void loadCurrent(String config){
		try {
			BsXXAuthEntity bsXXAuth=null;
			if(StringUtils.isNotEmpty(config)) {
				bsXXAuth =JSON.parseObject(config, BsXXAuthEntity.class);
			}else {
			    bsXXAuth = bsXXAuthService.loadDefaultOne();
			    redisUtils.set(Constant.CACHE_DEFAULT_XX_AUTH_CONFIG, JSON.toJSONString(bsXXAuth));
			}
			logger.info(String.format("loadCurrent config: %s", JSON.toJSONString(bsXXAuth)));
			if(bsXXAuth !=null) {
				XXAuthConfig=XXAuthConfig.toCurrent(bsXXAuth);
				redisUtils.delete(Constant.CACHE_REFRESH_TOKEN_XX);//清理刷新token
				redisUtils.delete(Constant.CACHE_ACCESS_TOKEN_XX);//清理访问token
			}
		} catch (Exception e) {
			// TODO: handle exception
			logger.error(String.format("loadCurrent config error:", e));
		}	
	}
	
	/**
	 * 获取默认证参数
	 * @param refresh
	 * @return
	 */
	public XXAuthConfig getXXAuthConfig() {
		reloadXXAuthConfig();
		return XXAuthConfig;
	}
	
	/**
	 *  认证参数配置
	 *  Class Name: XXAuthConfigScond.java
	 *  Description: 
	 *  DateTime 2019年8月30日 下午1:48:38 
	 *  @version 1.0
	 * 
	 */
	public static class XXAuthConfig  implements Serializable { 
		
	    private static final long serialVersionUID = -2350610782501632735L;
		 /**
	     * pin账号id
	     */
	    private String clientId;
	    /**
	     * pin账号密钥
	     */
	    private String clientSecret;
	    /**
	     * pin账号
	     */
	    private String userName;
	    /**
	     * pin密码
	     */
	    private String password;
	    /**
	     * 授权类型
	     */
	    private String grantType;
	    /**
	     * 范围
	     */
	    private String scope;	
		/**
		 * 校验
		 */
		private String check;
		
		public String getClientId() {
			return clientId;
		}

		public void setClientId(String clientId) {
			this.clientId = clientId;
		}

		public String getClientSecret() {
			return clientSecret;
		}

		public void setClientSecret(String clientSecret) {
			this.clientSecret = clientSecret;
		}

		public String getUserName() {
			return userName;
		}

		public void setUserName(String userName) {
			this.userName = userName;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public String getGrantType() {
			return grantType;
		}

		public void setGrantType(String grantType) {
			this.grantType = grantType;
		}

		public String getScope() {
			return scope;
		}

		public void setScope(String scope) {
			this.scope = scope;
		}

		
		//默认认证参数更新
		public static XXAuthConfig toCurrent(BsXXAuthEntity bsXXAuth) {
			logger.info(String.format("Loaded data from bsXXAuth: %s", JSON.toJSONString(bsXXAuth)));
			XXAuthConfig config = new XXAuthConfig();
			BeanUtils.copyProperties(bsXXAuth,config);
			return config;
		}

		@Override
		public String toString() {
			return "XXAuthConfig [clientId=" + clientId + ", clientSecret=" + clientSecret + ", userName=" + userName
					+ ", password=" + password + ", grantType=" + grantType + ", scope=" + scope + ", XXPriceType="
					+ XXPriceType + ", check=" + check + "]";
		}
		
	}
	
	
	
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值