當經(jīng)紀人創(chuàng)建客戶時,需要給對應的經(jīng)紀人增加戰(zhàn)報信息。在代碼層面上,客源的相關類只針對客源數(shù)據(jù)表操作。而戰(zhàn)報信息包含了多種業(yè)務統(tǒng)計數(shù)據(jù),客源只是其中統(tǒng)計的部分數(shù)據(jù)。鑒于兩者相對獨立,且客源的戰(zhàn)報信息會有所修改。因此,采用AOP+觀察者模式構建代碼。
網(wǎng)站建設哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、小程序定制開發(fā)、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了萬山免費建站歡迎大家使用!
定義一個注解,用于AOP攔截。
/**
* 戰(zhàn)報注解
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.PARAMETER})
@Documented
public @interface AchievementAnnotation {
OperateEnum operate() default OperateEnum.ADD;
enum OperateEnum{
ADD,UPDATE,DELETE
}
}
定義AOP,用戶獲取數(shù)據(jù),并轉發(fā)給觀察者
/**
* 戰(zhàn)報AOP
*/
@Aspect
@Component
public class AchievementAop {
/**
* 戰(zhàn)報觀察者列表
*/
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)報信息觀察者接口
* @param <T>
*/
public interface AchievementObserver<T> {
/**
* 是否支持該對象
* @param obj
* @return
*/
boolean isSupport(Object obj);
/**
* 操作業(yè)務數(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 {
// 實際業(yè)務處理
customerRpcService.saveAchievement(customerBase.getAgentUserId(), "ADD_CUSTOMER", customerBase.getId());
}
}
當前文章:觀察者模式+AOP代碼示例
網(wǎng)站地址:http://sd-ha.com/article24/jjsdce.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、微信小程序、面包屑導航、外貿(mào)建站、云服務器、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)