In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to make custom annotations in the SpringBoot environment to use ${xxx} expression, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
Correlation dependence
Org.aspectj aspectjrt org.aspectj aspectjweaver runtime
Custom annotation
@ Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.METHOD) @ Documented public @ interface Manufactur {String value () default "; / / manufacturer number}
AOP
AOP is required to parse the comments on the method in the method executor to obtain the corresponding value of the placeholder.
@ Component @ Aspect public class ManufacturAspect implements EnvironmentAware {private static final Logger logger = LoggerFactory.getLogger (ManufacturAspect.class); private Environment environment; @ Pointcut ("@ annotation (com.pack.annotation.Manufactur)") private void info () {} @ Before ("info ()") public void execBefore (JoinPoint jp) {MethodSignature sign = (MethodSignature) jp.getSignature (); Method method = sign.getMethod () Manufactur manu = method.getAnnotation (Manufactur.class); String value = manu.value (); logger.info ("get comment value: {}", value); BusinessService.code.set (this.environment.resolvePlaceholders (value));} @ Override public void setEnvironment (Environment environment) {this.environment = environment;}}
This class implements that EnvironmentAware is used to get the Environment object, which can get all the relevant configuration information in the current environment. At the same time, the content value corresponding to the placeholder can be parsed through the resolvePlaceholders method of this class.
Used in Service
@ Service public class BusinessService {public static ThreadLocal code = new ThreadLocal (); private static Logger logger = LoggerFactory.getLogger (BusinessService.class); @ Manufactur ("${manufactur.code}-# {1 + 3}") public String invoke (String id) {String sno = code.get (); logger.info ("dynamic acquisition of attribute values for custom annotations: {}", sno) / / todo return sno;}}
The parsed value is already stored in ThreadLocal in AOP.
test
@ RestController @ RequestMapping ("/ business") public class BusinessController {@ Resource private BusinessService bs; @ GetMapping ("/ {id}") public Object home (@ PathVariable String id) {return bs.invoke (id);}}
It is very simple to support placeholders in this custom annotation.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.