观察者模式+AOP 代码示例
发表于:2024-10-18 作者:千家信息网编辑
千家信息网最后更新 2024年10月18日,背景当经纪人创建客户时,需要给对应的经纪人增加战报信息。在代码层面上,客源的相关类只针对客源数据表操作。而战报信息包含了多种业务统计数据,客源只是其中统计的部分数据。鉴于两者相对独立,且客源的战报信息
千家信息网最后更新 2024年10月18日观察者模式+AOP 代码示例
背景
当经纪人创建客户时,需要给对应的经纪人增加战报信息。在代码层面上,客源的相关类只针对客源数据表操作。而战报信息包含了多种业务统计数据,客源只是其中统计的部分数据。鉴于两者相对独立,且客源的战报信息会有所修改。因此,采用AOP+观察者模式构建代码。
代码
定义一个注解,用于AOP拦截。
/** * 战报注解 */@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.METHOD, ElementType.PARAMETER})@Documentedpublic @interface AchievementAnnotation { OperateEnum operate() default OperateEnum.ADD; enum OperateEnum{ ADD,UPDATE,DELETE }}
定义AOP,用户获取数据,并转发给观察者
/** * 战报AOP */@Aspect@Componentpublic class AchievementAop { /** * 战报观察者列表 */ 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); } }}
定义观察者通用接口
/** * 战报信息观察者接口 * @param */public interface AchievementObserver { /** * 是否支持该对象 * @param obj * @return */ boolean isSupport(Object obj); /** * 操作业务数据 * @param t * @throws RuntimeException */ void execute(T t) throws RuntimeException;}
客源观察者
/** * 客源信息的观察者 */@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 { // 实际业务处理 customerRpcService.saveAchievement(customerBase.getAgentUserId(), "ADD_CUSTOMER", customerBase.getId()); }}
观察者
观察
客源
战报
数据
信息
代码
业务
接口
注解
经纪人
经纪
统计
模式
只是
多种
实际
客户
对象
层面
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
国家网络安全法海报
中国移动如何做好网络安全
数据库管理信息技术
adodc控制数据库
哈尔滨网络技术人员
服务器集群方式
数据库安全策略重要
软件开发调研问卷
与数据库有关的服务
软件开发开月度例会的内容
iphone服务器链接出现问题
怎么把数据添加到数据库
奉贤区app软件开发
网络安全知识留言
软件开发算高薪吗
游聚服务器
中国石化电子邮箱服务器
地级市软件开发
临沂市甲设网络技术有限公司
数据库单个实体型
网络安全标识解析技术
我的世界服务器存档视频
服务器网卡的安装方法
网络安全人防技防相结合主动防御
u8数据库取消坏账
宁国市网络安全吗
腾讯服务器一般多少钱
服务器为什么会一个卡一个不卡
太原软件开发工资
自建管理服务器