Skip to content

Commit 1cd3686

Browse files
committed
fix: 工具类注入逻辑优化、log 优化
1 parent 7beb7b1 commit 1cd3686

2 files changed

Lines changed: 51 additions & 16 deletions

File tree

uni-boot-common/src/main/java/top/cadecode/uniboot/common/util/JacksonUtil.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
import com.fasterxml.jackson.databind.DeserializationFeature;
88
import com.fasterxml.jackson.databind.ObjectMapper;
99
import com.fasterxml.jackson.databind.SerializationFeature;
10-
import lombok.RequiredArgsConstructor;
10+
import lombok.extern.slf4j.Slf4j;
1111
import org.springframework.beans.factory.InitializingBean;
12+
import org.springframework.beans.factory.annotation.Autowired;
1213
import org.springframework.stereotype.Component;
1314

1415
import java.io.IOException;
@@ -21,16 +22,21 @@
2122
* @author Cade Li
2223
* @date 2022/5/23
2324
*/
24-
@RequiredArgsConstructor
25+
@Slf4j
2526
@Component
2627
public class JacksonUtil implements InitializingBean {
2728

29+
public static ObjectMapper OBJECT_MAPPER;
30+
31+
private ObjectMapper objectMapper;
32+
2833
/**
2934
* 注入 Spring 管理的 ObjectMapper
3035
*/
31-
private final ObjectMapper objectMapper;
32-
33-
public static ObjectMapper OBJECT_MAPPER;
36+
@Autowired(required = false)
37+
public void setObjectMapper(ObjectMapper objectMapper) {
38+
this.objectMapper = objectMapper;
39+
}
3440

3541
/**
3642
* 转换对象到 json 串
@@ -62,7 +68,7 @@ public static String toJson(Object bean, boolean isPretty) {
6268
}
6369
return OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(bean);
6470
} catch (JsonProcessingException e) {
65-
throw new RuntimeException("转换 Bean 到 Json 出错", e);
71+
throw new RuntimeException("cast bean to json fail", e);
6672
}
6773
}
6874

@@ -84,7 +90,7 @@ public static <T> T toBean(String json, Class<T> clazz) {
8490
try {
8591
return OBJECT_MAPPER.readValue(json, clazz);
8692
} catch (Exception e) {
87-
throw new RuntimeException("转换 Json 到 Bean 出错", e);
93+
throw new RuntimeException("cast json to bean fail", e);
8894
}
8995
}
9096

@@ -106,14 +112,15 @@ public static <T> T toBean(String json, TypeReference<T> typeReference) {
106112
try {
107113
return OBJECT_MAPPER.readValue(json, typeReference);
108114
} catch (IOException e) {
109-
throw new RuntimeException("转换 Json 到 Bean 出错", e);
115+
throw new RuntimeException("cast json to bean fail", e);
110116
}
111117
}
112118

113119
@Override
114120
public void afterPropertiesSet() {
115121
OBJECT_MAPPER = this.objectMapper;
116122
if (Objects.isNull(OBJECT_MAPPER)) {
123+
log.warn("Bean objectMapper not found, use default config by JacksonUtil");
117124
OBJECT_MAPPER = new ObjectMapper();
118125
// 定义日期格式
119126
String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";

uni-boot-common/src/main/java/top/cadecode/uniboot/common/util/RedisUtil.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package top.cadecode.uniboot.common.util;
22

33
import com.fasterxml.jackson.core.type.TypeReference;
4-
import lombok.RequiredArgsConstructor;
54
import org.springframework.beans.factory.InitializingBean;
5+
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.data.redis.core.StringRedisTemplate;
77
import org.springframework.stereotype.Component;
88

@@ -15,16 +15,20 @@
1515
* @author Cade Li
1616
* @date 2022/5/29
1717
*/
18-
@RequiredArgsConstructor
1918
@Component
2019
public class RedisUtil implements InitializingBean {
2120

21+
public static StringRedisTemplate TEMPLATE;
22+
23+
private StringRedisTemplate stringRedisTemplate;
24+
2225
/**
2326
* 自动注入 stringRedisTemplate
2427
*/
25-
private final StringRedisTemplate stringRedisTemplate;
26-
27-
public static StringRedisTemplate TEMPLATE;
28+
@Autowired(required = false)
29+
public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
30+
this.stringRedisTemplate = stringRedisTemplate;
31+
}
2832

2933
/**
3034
* 获取 value
@@ -45,21 +49,45 @@ public static <T> T get(String key, TypeReference<T> typeReference) {
4549
/**
4650
* 添加 key
4751
*/
52+
public static void set(String key, Object o) {
53+
String json = JacksonUtil.toJson(o);
54+
TEMPLATE.opsForValue().set(key, json);
55+
}
56+
57+
/**
58+
* 添加 key,如果不存在
59+
*/
60+
public static Boolean setIfAbsent(String key, Object o) {
61+
String json = JacksonUtil.toJson(o);
62+
return TEMPLATE.opsForValue().setIfAbsent(key, json);
63+
}
64+
65+
/**
66+
* 添加 key,如果不存在
67+
*/
68+
public static Boolean setIfPresent(String key, Object o) {
69+
String json = JacksonUtil.toJson(o);
70+
return TEMPLATE.opsForValue().setIfPresent(key, json);
71+
}
72+
73+
/**
74+
* 添加 key 并设置有效期
75+
*/
4876
public static void set(String key, Object o, long timeout, TimeUnit timeUnit) {
4977
String json = JacksonUtil.toJson(o);
5078
TEMPLATE.opsForValue().set(key, json, timeout, timeUnit);
5179
}
5280

5381
/**
54-
* 添加 key,如果不存在
82+
* 添加 key 并设置有效期,如果不存在
5583
*/
5684
public static Boolean setIfAbsent(String key, Object o, long timeout, TimeUnit timeUnit) {
5785
String json = JacksonUtil.toJson(o);
5886
return TEMPLATE.opsForValue().setIfAbsent(key, json, timeout, timeUnit);
5987
}
6088

6189
/**
62-
* 添加 key,如果不存在
90+
* 添加 key 并设置有效期,如果不存在
6391
*/
6492
public static Boolean setIfPresent(String key, Object o, long timeout, TimeUnit timeUnit) {
6593
String json = JacksonUtil.toJson(o);
@@ -91,7 +119,7 @@ public static Boolean expire(String key, long timeout, TimeUnit timeUnit) {
91119
public void afterPropertiesSet() {
92120
TEMPLATE = stringRedisTemplate;
93121
if (Objects.isNull(TEMPLATE)) {
94-
throw new IllegalArgumentException("无法注入依赖:stringRedisTemplate");
122+
throw new IllegalArgumentException("Bean stringRedisTemplate not found");
95123
}
96124
}
97125
}

0 commit comments

Comments
 (0)