Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Observer mode + AOP code example

Shulou Source: shulou.com Published: 2022-06-03 05:57:40 09月10日 Update

Background

When a broker creates a client, it needs to add war report information to the corresponding broker. At the code level, the related classes of the guest source only operate on the guest source data table. The war report information contains a variety of business statistics, and the tourist source is only part of the statistics. In view of the fact that the two are relatively independent, and the war report information of the tourist source will be modified. Therefore, the code is built in AOP+ Observer mode.

Code

Define an annotation for AOP interception.

/ * Retention (RetentionPolicy.RUNTIME) @ Target ({ElementType.METHOD, ElementType.PARAMETER}) @ Documentedpublic @ interface AchievementAnnotation {OperateEnum operate () default OperateEnum.ADD; enum OperateEnum {ADD,UPDATE,DELETE}}

Define AOP, where the user acquires the data and forwards it to the observer

/ * * War report AOP * / @ Aspect@Componentpublic class AchievementAop {/ * War report Observer list * / private List observerList; public AchievementAop () {this.observerList = new ArrayList ();} public List getObserverList () {return observerList;} public void setObserverList (List observerList) {if (null! = this.observerList) this.observerList.addAll (observerList) This.observerList = observerList;} / * Observer injected into the guest source * / @ Autowired public void setCustomerAchievementObserver (CustomerAchievementObserver customerAchievementObserver) {getObserverList () .add (customerAchievementObserver) } @ Pointcut ("@ annotation (com.pretang.cloud.aop.AchievementAnnotation)") private void pointCut () {} @ AfterReturning (pointcut = "pointCut ()", returning = "retVal") public void after (JoinPoint joinPoint, Object retVal) {Signature signature = joinPoint.getSignature (); MethodSignature methodSignature = (MethodSignature) signature; Method targetMethod = methodSignature.getMethod (); AchievementAnnotation annotation = targetMethod.getAnnotation (AchievementAnnotation.class) AchievementAnnotation.OperateEnum operateEnum = annotation.operate (); for (AchievementObserver observer: observerList) {if (observer.isSupport (retVal)) observer.execute (retVal);}

Define a common interface for observers

/ * * War report Information Observer Interface * @ param * / public interface AchievementObserver {/ * whether the object is supported * @ param obj * @ return * / boolean isSupport (Object obj); / * manipulate business data * @ param t * @ throws RuntimeException * / void execute (T t) throws RuntimeException;}

Tourist source observer

/ * * Observer of guest source information * / @ Componentpublic class CustomerAchievementObserver implements AchievementObserver {@ Autowired private CustomerRpcService customerRpcService; @ Override public boolean isSupport (Object obj) {return obj instanceof CustomerBase;} @ Override public void execute (CustomerBase customerBase) throws RuntimeException {/ / actual business processing customerRpcService.saveAchievement (customerBase.getAgentUserId (), "ADD_CUSTOMER", customerBase.getId ());}}

Tags: Observer observation customer source war report data information code business interface annotation broker broker statistics model just multiple actual customer object level Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS vpn MySQL Huawei Apple