Get the App
SLTechnology News&Howtos  ›  Development  › 

How to enable custom annotations to use ${xxx} expressions in the SpringBoot environment

Shulou Source: shulou.com Published: 2022-06-03 08:47:52 11月15日 Update

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.

Tags: Comments environment content objects methods help support expressions clarity lieutenant information dynamic vendor at the same time each other for this properties articles novices more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno Microsoft Shulou Technology Redmi