<!-- springboot parent -->
<parent>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Cairo-SR7</version>
<relativePath/>
</parent>
<!-- springboot 项目添加此parent后,下列工具类中,除第一个外,其他均不需要指定版本,且不用担心jar包冲突 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!-- 工具类 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.16</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.12</version>
</dependency>
import org.springframework.beans.BeanUtils;
package com.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* 手动获取spring管理的ioc bean实例工具类
*
*/
@Slf4j
@Component
public class SpringBeanUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/**
* 获取applicationContext对象
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringBeanUtils.applicationContext == null) {
SpringBeanUtils.applicationContext = applicationContext;
}
log.info("Starting Initialization ApplicationContext Object Successfully.");
}
/**
* 根据bean名称获取实例
*
* @param name bean名称
* @return
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* 通过类型获取ioc实例
*
* @param clazz 类型
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
/**
* 通过名称 & 类型获取类型
*
* @param name 名称
* @param clazz 类型
* @param <T>
* @return
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
package com.util;
import com.google.common.collect.Maps;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.util.Map;
import java.util.Properties;
/**
* <p>
* <code>CachePropertiesUtils</code>
* </p>
* Description: 读取配置文件,具有缓存提高性能
* TODO: 监听文件,及时更新
*
*/
@Slf4j
public class CachePropertiesUtils {
/** 默认配置文件 */
private static final String DEFAULT_FILE = "config/config.properties";
/** 默认值 */
private static final String DEFAULT_VALUE = null;
/** 缓存 */
private static Map<String, Properties> cacheMap = Maps.newHashMap();
private CachePropertiesUtils() {
throw new IllegalStateException("Utility class");
}
public static String getConfigValue(@NonNull String key) {
return getValue(DEFAULT_FILE, key);
}
public static String getConfigValue(@NonNull String key, String defaultValue) {
return getValue(DEFAULT_FILE, key, defaultValue);
}
public static String getValue(@NonNull String fileName, @NonNull String key) {
return getPropertiesValue(fileName, key, DEFAULT_VALUE);
}
public static String getValue(@NonNull String fileName, @NonNull String key, String defaultValue) {
return getPropertiesValue(fileName, key, defaultValue);
}
private static String getPropertiesValue(String fileName, String key, String defaultValue) {
try {
Properties properties = cacheMap.get(fileName);
if (properties == null) {
properties = PropertiesLoaderUtils.loadAllProperties(fileName);
cacheMap.put(fileName, properties);
}
return properties.getProperty(key);
} catch (Exception e) {
log.error(String.format("获取【%s】中【%s】失败:", fileName, key), e);
return defaultValue;
}
}
}
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/** 异步操作记录日志的线程池 */
private final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10,
new ThreadFactoryBuilder().setNameFormat("name-pool-thread-%d").build());
/**
* 记录用户登录结果
* <P>异步,延迟delay时间后执行</P>
*
* @param loginResult 登录结果
* @param delay 延迟时间
* @param unit 时间单位
* @date 2021-01-20 上午 08:51
*/
public void saveLoginResultMsg(final String loginResult, int delay, TimeUnit unit) {
log.info("==> login result:{}", loginResult);
executor.schedule(new TimerTask() {
@Override
public void run() {
try {
// 保存数据到redis或者数据库
} catch (Exception e) {
log.error("==>saveLoginResultMsg error,loginResult:{}", loginResult, e);
}
}
}, delay, unit);
}