日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SpringBoot自定義注解屬性支持占位符$「x」

 環(huán)境:SpringBoot2.3.8.RELEASE + JDK1.8

本文教你如何在SpringBoot環(huán)境下使得自定義的注解能夠使用${xxx}表達式。

相關(guān)依賴

 
 
 
 
  1.  
  2.             org.aspectj 
  3.             aspectjrt 
  4.  
  5.  
  6.             org.aspectj 
  7.             aspectjweaver 
  8.             runtime 
  9.  

 自定義注解

 
 
 
 
  1. @Retention(RetentionPolicy.RUNTIME) 
  2. @Target(ElementType.METHOD) 
  3. @Documented 
  4. public @interface Manufactur { 
  5.     String value() default "" ; // 廠商編號 

 AOP

需要AOP在方法執(zhí)行器對方法上的注解進行解析處理,獲取占位符對應的值

 
 
 
 
  1. @Component 
  2. @Aspect 
  3. public class ManufacturAspect implements EnvironmentAware { 
  4.      
  5.     private static final Logger logger = LoggerFactory.getLogger(ManufacturAspect.class) ; 
  6.      
  7.     private Environment environment; 
  8.      
  9.     @Pointcut("@annotation(com.pack.annotation.Manufactur)") 
  10.     private void info() {} 
  11.      
  12.     @Before("info()") 
  13.     public void execBefore(JoinPoint jp) { 
  14.         MethodSignature sign = (MethodSignature) jp.getSignature() ; 
  15.         Method method = sign.getMethod() ; 
  16.         Manufactur manu = method.getAnnotation(Manufactur.class) ; 
  17.         String value = manu.value() ; 
  18.         logger.info("獲取到注解值:{}", value) ; 
  19.         BusinessService.code.set(this.environment.resolvePlaceholders(value)) ; 
  20.     } 
  21.  
  22.     @Override 
  23.     public void setEnvironment(Environment environment) { 
  24.         this.environment = environment ; 
  25.     } 
  26.      

 該類實現(xiàn)了EnvironmentAware 用于獲取Environment對象,該對象能夠獲取當前環(huán)境下的所有相關(guān)配置信息。同時通過該類的resolvePlaceholders方法能夠解析占位符對應的內(nèi)容值。

Service中使用

 
 
 
 
  1. @Service 
  2. public class BusinessService { 
  3.      
  4.     public static ThreadLocal code = new ThreadLocal() ; 
  5.      
  6.     private static Logger logger = LoggerFactory.getLogger(BusinessService.class) ; 
  7.      
  8.     @Manufactur("${manufactur.code}-#{1 + 3}") 
  9.     public String invoke(String id) { 
  10.         String sno = code.get() ; 
  11.         logger.info("自定義注解動態(tài)獲取屬性值:{}", sno) ; 
  12.         // todo 
  13.         return sno ; 
  14.     } 
  15.      

 在AOP中將解析后的值已經(jīng)存入到了ThreadLocal中。

測試

 
 
 
 
  1. @RestController 
  2. @RequestMapping("/business") 
  3. public class BusinessController { 
  4.      
  5.     @Resource 
  6.     private BusinessService bs ; 
  7.      
  8.     @GetMapping("/{id}") 
  9.     public Object home(@PathVariable String id) { 
  10.         return bs.invoke(id) ; 
  11.     } 
  12.      

到此一個自定義注解中支持占位符就完成了,還是非常簡單的。


網(wǎng)頁題目:SpringBoot自定義注解屬性支持占位符$「x」
路徑分享:http://m.5511xx.com/article/cohedoo.html