类名后面加上 implements CommandLineRunner 表示启动时调用方法
@Override
public void run(String... args) throws Exception
启动时调用的方法
package com.lunz.amp.translator.commom;
import com.alibaba.fastjson.JSON;
import com.lunz.amp.core.util.LogUtil;
import com.lunz.amp.translator.commom.cache.RedisCache;
import com.lunz.amp.translator.service.CategoryWordsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
public class WordsType /*implements CommandLineRunner*/ {
@Autowired
private RedisCache redisCache;
@Autowired
private CategoryWordsService categoryWordsService;
//====================敏感词===================
/**
* 一般敏感词
*/
public void setCommomWords() {
try {
List<String> getCommom = categoryWordsService.getCategoryWords(1);
if (getCommom.size() > 0) {
String str = JSON.toJSONString(getCommom);
String key = RedisConstants.RedisKeyPrefix.CREATE_WORDS_COMMON;
redisCache.set(key, str, -1, TimeUnit.MINUTES);
} else {
log.error("缓存->存储一般敏感词出错,获取失败!");
}
} catch (Exception e) {
log.error("缓存->存储省一般敏感词出错,错误信息【{}】,堆栈信息【{}】", e.getMessage(), LogUtil.convertStackInfoToStr(e));
}
}
/**
* 严重敏感词
*/
public void setSeriousWords() {
try {
List<String> getSerious = categoryWordsService.getCategoryWords(2);
if (getSerious.size() > 0) {
String str = JSON.toJSONString(getSerious);
String key = RedisConstants.RedisKeyPrefix.CREATE_WORDS_SERIOUS;
redisCache.set(key, str, -1, TimeUnit.MINUTES);
} else {
log.error("缓存->存储严重敏感词出错,获取失败!");
}
} catch (Exception e) {
log.error("缓存->存储严重敏感词出错,错误信息【{}】,堆栈信息【{}】", e.getMessage(), LogUtil.convertStackInfoToStr(e));
}
}
/**
* 致命敏感词
*/
public void setFatalWords() {
try {
List<String> getFatal = categoryWordsService.getCategoryWords(3);
if (getFatal.size() > 0) {
String str = JSON.toJSONString(getFatal);
String key = RedisConstants.RedisKeyPrefix.CREATE_WORDS_FATAL;
redisCache.set(key, str, -1, TimeUnit.MINUTES);
} else {
log.error("缓存->存储致命敏感词出错,获取失败!");
}
} catch (Exception e) {
log.error("缓存->存储致命敏感词出错,错误信息【{}】,堆栈信息【{}】", e.getMessage(), LogUtil.convertStackInfoToStr(e));
}
}
/**
* 刷新敏感词
*/
public void updateWords(Integer id) {
switch (id) {
case 1:
redisCache.delete(RedisConstants.RedisKeyPrefix.CREATE_WORDS_COMMON);
setCommomWords();
break;
case 2:
redisCache.delete(RedisConstants.RedisKeyPrefix.CREATE_WORDS_SERIOUS);
setSeriousWords();
break;
case 3:
redisCache.delete(RedisConstants.RedisKeyPrefix.CREATE_WORDS_FATAL);
setFatalWords();
break;
}
}
/* @Override
public void run(String... args) throws Exception {
setCommomWords();
setSeriousWords();
setFatalWords();
}
*/
}