十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶(hù) + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專(zhuān)業(yè)推廣+無(wú)憂(yōu)售后,網(wǎng)站問(wèn)題一站解決
當(dāng)經(jīng)紀(jì)人創(chuàng)建客戶(hù)時(shí),需要給對(duì)應(yīng)的經(jīng)紀(jì)人增加戰(zhàn)報(bào)信息。在代碼層面上,客源的相關(guān)類(lèi)只針對(duì)客源數(shù)據(jù)表操作。而戰(zhàn)報(bào)信息包含了多種業(yè)務(wù)統(tǒng)計(jì)數(shù)據(jù),客源只是其中統(tǒng)計(jì)的部分?jǐn)?shù)據(jù)。鑒于兩者相對(duì)獨(dú)立,且客源的戰(zhàn)報(bào)信息會(huì)有所修改。因此,采用AOP+觀察者模式構(gòu)建代碼。
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、小程序定制開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶(hù)創(chuàng)新互聯(lián)還提供了萬(wàn)山免費(fèi)建站歡迎大家使用!
定義一個(gè)注解,用于AOP攔截。
/**
* 戰(zhàn)報(bào)注解
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.PARAMETER})
@Documented
public @interface AchievementAnnotation {
OperateEnum operate() default OperateEnum.ADD;
enum OperateEnum{
ADD,UPDATE,DELETE
}
}
定義AOP,用戶(hù)獲取數(shù)據(jù),并轉(zhuǎn)發(fā)給觀察者
/**
* 戰(zhàn)報(bào)AOP
*/
@Aspect
@Component
public class AchievementAop {
/**
* 戰(zhàn)報(bào)觀察者列表
*/
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;
}
/**
*注入客源的觀察者
*/
@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);
}
}
}
定義觀察者通用接口
/**
* 戰(zhàn)報(bào)信息觀察者接口
* @param
*/
public interface AchievementObserver {
/**
* 是否支持該對(duì)象
* @param obj
* @return
*/
boolean isSupport(Object obj);
/**
* 操作業(yè)務(wù)數(shù)據(jù)
* @param t
* @throws RuntimeException
*/
void execute(T t) throws RuntimeException;
}
客源觀察者
/**
* 客源信息的觀察者
*/
@Component
public 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 {
// 實(shí)際業(yè)務(wù)處理
customerRpcService.saveAchievement(customerBase.getAgentUserId(), "ADD_CUSTOMER", customerBase.getId());
}
}