In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.