當(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<AchievementObserver> observerList;
public AchievementAop() {
this.observerList = new ArrayList<>();
}
public List<AchievementObserver> getObserverList() {
return observerList;
}
public void setObserverList(List<AchievementObserver> 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 <T>
*/
public interface AchievementObserver<T> {
/**
* 是否支持該對(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<CustomerBase> {
@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());
}
}
當(dāng)前文章:觀察者模式+AOP代碼示例
網(wǎng)站地址:http://bm7419.com/article24/jjsdce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、微信小程序、面包屑導(dǎo)航、外貿(mào)建站、云服務(wù)器、小程序開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)