In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use Springboot custom annotations and support SPEL expressions, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.
Springboot custom annotation, support SPEL expression
For example, custom redis fuzzy delete annotation
1. custom annotation import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java. lang.annotation.RetentionPolicy;import java.lang.annotation.Target; @Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface CacheEvictFuzzy { /** * redis key set, fuzzy deletion * @return */ String[] key() default ""; }2. Resolve annotation parameters using AOP intercept method import org.apache.commons.lang3.StringUtils;import org.aspectj.lang. annotation.AfterThrowing;import org. aspectj.lang.annotation.Around; import org. aspectj.lang. annotation.Aspect;import org. aspectj. lang. annotation.Pointcut;import org. aspectj. lang.reflect. Signature;import org.slf4j.Logger;import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.LocalVariableTableParameterNameDiscoverer;import org.springframework.core.annotation.Order;import org.springframework.expression.ExpressionParser;import org.springframework.expression.spel.standard.SpelExpressionParser;import org.springframework.expression.spel.support.StandardEvaluationContext;import org.springframework.stereotype.Component; import java.lang.reflect.Method;import java.util.Set;@Aspect@Order(1)@Componentpublic class CacheCleanFuzzyAspect { Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private RedisUtil redis; //Specify the method to perform AOP @Pointcut(value = "@annotation(cacheEvictFuzzy)") public void pointCut(CacheEvictFuzzy cacheEvictFuzzy){} //Set the slice to a method annotated with @RedisCacheable @Around("@annotation(cacheEvictFuzzy)") public Object around(ProceedingJoinPoint proceedingJoinPoint, CacheEvictFuzzy cacheEvictFuzzy){ return doRedis(proceedingJoinPoint, cacheEvictFuzzy); } @AfterThrowing(pointcut = "@annotation(cacheEvictFuzzy)", throwing = "error") public void afterThrowing (Throwable error, CacheEvictFuzzy cacheEvictFuzzy){ logger.error(error.getMessage()); } /** * delete the cache * @param proceedingJoinPoint * @param cacheEvictFuzzy * @return */ private Object doRedis (ProceedingJoinPoint proceedingJoinPoint, CacheEvictFuzzy cacheEvictFuzzy){ Object result = null; //Get the parameter list of the method modified by the cut Object[] args = proceedingJoinPoint.getArgs(); //get proxy method Method method = ((MethodSignature) proceedingJoinPoint.getSignature()).getMethod(); String[] keys = cacheEvictFuzzy.key(); Set keySet = null; String realkey = ""; for (int i = 0; i
< keys.length; i++) { if (StringUtils.isBlank(keys[i])){ continue; } realkey = parseKey(keys[i], method, args); keySet = redis.keys("*"+realkey+"*"); if (null != keySet && keySet.size()>0){ redis.delKeys(keySet); logger.debug("intercepted method: " + proceedingJoinPoint.getSignature().getName() + "method"); logger.debug("deleted data key is: "+keySet.toString()); } } try { result = proceedingJoinPoint.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); }finally { return result; } } /** * Get cached key * key is defined on the annotation, supporting SPEL expressions * @return */ private String parseKey(String key, Method method, Object [] args){ if(StringUtils.isEmpty(key)) return null; //Get a list of intercepted method parameter names (using Spring support libraries) LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer(); String[] paraNameArr = u.getParameterNames(method); //Use SPEL to parse keys ExpressionParser parser = new SpelExpressionParser(); //SPEL context StandardEvaluationContext context = new StandardEvaluationContext(); //Put method parameters into SPEL context for(int i=0;i
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.