24
24
import org .aspectj .lang .annotation .Pointcut ;
25
25
import org .slf4j .Logger ;
26
26
import org .slf4j .LoggerFactory ;
27
- import org .springframework .beans .factory .InitializingBean ;
28
- import org .springframework .beans .factory .annotation .Autowired ;
27
+ import org .springframework .beans .BeansException ;
29
28
import org .springframework .cache .Cache ;
30
29
import org .springframework .cache .Cache .ValueWrapper ;
31
30
import org .springframework .cache .CacheManager ;
32
31
import org .springframework .cache .guava .GuavaCacheManager ;
33
32
import org .springframework .cache .support .SimpleValueWrapper ;
34
- import org .springframework .core .annotation .Order ;
33
+ import org .springframework .context .ApplicationContext ;
34
+ import org .springframework .context .ApplicationContextAware ;
35
35
import org .springframework .stereotype .Component ;
36
36
import org .springframework .util .CollectionUtils ;
37
37
45
45
46
46
@ Aspect
47
47
@ Component
48
- @ Order (200 )
49
- public class AopCache implements InitializingBean {
48
+ public class AopCache implements ApplicationContextAware {
49
+ public static CacheManager cacheManager = null ;
50
+ public static ApplicationContext springContext = null ;
50
51
private static Logger log = LoggerFactory .getLogger (AopCache .class );
51
- private @ Autowired (required =false ) CacheManager cacheManager ;
52
52
private int cacheStatus = -1 ;//-1待初始化 0不可用 1可用
53
53
private String [] cacheMethods = {"list" , "count" , "get" };
54
54
private String [] evictMethods = {"add" , "batchAdd" , "update" , "delete" , "delBy" };
55
55
private Map <String , Boolean > classNames = new HashMap <>();
56
56
private Map <String , Class <?>> returnTypes = new HashMap <>();
57
57
58
+ /**
59
+ * 尝试获取bean,或者返回null
60
+ * @param clazz
61
+ * @return
62
+ */
63
+ public static <T > T getBean (Class <T > clazz ) {
64
+ try {
65
+ return springContext .getBean (clazz );
66
+ }catch (Exception e ) {
67
+ return null ;
68
+ }
69
+ }
70
+
58
71
@ Pointcut ("within(com.dev.base.mybatis.service.BaseMybatisService+)" )
59
72
public void cacheService (){}
60
73
@@ -108,7 +121,9 @@ public void run() {
108
121
}
109
122
110
123
@ Override
111
- public void afterPropertiesSet () throws Exception {
124
+ public void setApplicationContext (ApplicationContext applicationContext ) throws BeansException {
125
+ springContext = applicationContext ;
126
+ cacheManager = getBean (CacheManager .class );
112
127
initCacheManager ();
113
128
}
114
129
@@ -204,7 +219,7 @@ public RedisCacheManager(JedisPool jedisPool, int expire) {
204
219
205
220
@ Override
206
221
public Cache getCache (String name ) {
207
- RedisCache redisCache = redisCaches .get (name );
222
+ RedisCache redisCache = redisCaches .get (name = StringUtils . trimToEmpty ( name ) );
208
223
if (redisCache ==null ) {
209
224
redisCache = new RedisCache (this , name );
210
225
redisCaches .put (name , redisCache );
@@ -226,7 +241,7 @@ public <T> T execute(JedisCallback<T> callback) {
226
241
}
227
242
228
243
public static byte [] byteKey (String cache , String key ) {
229
- return (cache +":" +key ).getBytes (StandardCharsets .UTF_8 );
244
+ return (StringUtils . isBlank ( cache ) ? key : cache +":" +key ).getBytes (StandardCharsets .UTF_8 );
230
245
}
231
246
232
247
public static byte [] byteValue (Object value ) {
@@ -334,7 +349,7 @@ public String doInJedis(Jedis jedis) {
334
349
});
335
350
}
336
351
private String keystr (Object key ) {
337
- return new StringBuilder (name ). append ( ":" ). append (key ). toString ( );
352
+ return StringUtils . isBlank (name ) ? String . valueOf ( key ) : name + ":" + String . valueOf (key );
338
353
}
339
354
}
340
355
0 commit comments