AOP通過開啟註解方式自動注入值
阿新 • • 發佈:2022-03-08
註解:EnableDimEnhance
package com.comma.teeth.enhance.dim.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * <p> * * </p> * * @author: GoslingWu * @date: 2022-03-08 */ @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface EnableDimEnhance { boolean enable() default true; }
AOP:DimEnhanceAop
package com.comma.teeth.conf; import com.comma.teeth.common.vo.Paging; import com.comma.teeth.enhance.dim.annotation.EnableDimEnhance; import com.comma.teeth.enhance.dim.service.DimEnhanceService; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Collection; /** * <p> * * </p> * * @author: GoslingWu * @date: 2022-03-08 */ @Data @Slf4j @Aspect @Component public class DimEnhanceAop { /** * 切點 全部service方法 */ //private static final String POINTCUT = "execution(public * com.comma.teeth.*.service.impl..*.*(..))"; @Autowired private DimEnhanceService dimEnhanceService; @Pointcut("@annotation(com.comma.teeth.enhance.dim.annotation.EnableDimEnhance)") public void dimEnhanceAspect() { } @AfterReturning(pointcut = "dimEnhanceAspect()", returning = "result") public void afterReturning(JoinPoint joinPoint, Object result) { if (result == null) { return; } // 方式1:service方法上開啟自動注入 { // 獲取註解 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); // 獲取當前方法被註解註釋的註解物件 EnableDimEnhance annotation = signature.getMethod().getAnnotation(EnableDimEnhance.class); if (annotation.enable()) { if (result instanceof Paging) { Paging iPage = (Paging) result; //通過反射獲取指定屬性上的註解,@SysDictField(code="1",valueFieldName="typeName")去自動注入 dimEnhanceService.autoFillValue(iPage.getRecords()); } else if (result instanceof Collection) { dimEnhanceService.autoFillValue((Collection) result); } else{ dimEnhanceService.autoFillValue(result); } } } // 方式2:返回實體類上開啟自動注入 /*{ if (result instanceof Paging) { Paging iPage = (Paging) result; List records = iPage.getRecords(); if(CollectionUtil.isNotEmpty(records)){ Object o = records.get(0); EnableDimEnhance annotation = o.getClass().getAnnotation(EnableDimEnhance.class); if(annotation.enable()){ dimEnhanceService.autoFillValue(iPage.getRecords()); } } } else { EnableDimEnhance annotation = result.getClass().getAnnotation(EnableDimEnhance.class); if(annotation.enable()){ dimEnhanceService.autoFillValue(result); } } }*/ log.info("DimEnhanceAop-------afterReturning:{}", result); } }
SysDictField
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SysDictField {
String code();
/**
* 用來指定自動填充時的欄位,valueField必須是字串型別。
* <p>
* 預設為:註解所在欄位名 + "Name"
*
* @return .
*/
String valueFieldName() default "";
}
@Data @Builder public class CounselorFollowRecordQueryVo implements Serializable{ private static final long serialVersionUID = 1L; @SysDictField(code = SysDictConstant.COMMUNICATION, valueFieldName = "typeName") @ApiModelProperty(value = "跟進方式id") private Integer typeId; @ApiModelProperty(value = "跟進方式") private String typeName; }
使用
@Override
//使用註解標識,會進入AOP,把Vo物件中的@SysDictField屬性標識指定的valueFieldName名稱填充進去,通過反射(sql中typeId是要查詢出來的,反射只填充name)
//我們有些表中有crrentUserId,一樣可以通過自定義一個@UserField來實現UserName的填充
@EnableDimEnhance
public Paging<CounselorFollowRecordQueryVo> getCounselorFollowRecordPageList(CounselorFollowRecordQueryParam param) throws Exception {
Page page = setPageParam(param, OrderItem.desc("id"));
IPage<CounselorFollowRecordQueryVo> iPage = counselorFollowRecordMapper.getCounselorFollowRecordPageList(page, param, labelSearchParam);
return new Paging<>(iPage);
}