当前位置: 首页 > news >正文

003 静态代理

文章目录

    • StudentServiceImpl
    • StudentService.java
    • StudentServiceProxy.java
    • StudentServiceProxy1.java
    • StudentServiceProxyTest.java

StudentServiceImpl


package com.aistart.service.impl;import com.aistart.mapper.StudentMapper;
import com.aistart.pojo.Student;
import com.aistart.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*注解写在实现类中*//*被代理的类,外部不能直接访问,而是让代理访问,目标类*/
public class StudentServiceImpl implements StudentService {@Overridepublic boolean addStu() {/*理论上,这里有个日志*/System.out.println("service调用执行,增加另一个学生");/*理论上,这里有个日志*/return false;}@Overridepublic boolean delStu() {System.out.println("service调用执行,删除另一个学生");return false;}
}

StudentService.java

package com.aistart.service;import com.aistart.pojo.Student;public interface StudentService {boolean addStu();boolean delStu();
}

StudentServiceProxy.java


package com.aistart.proxy;import com.aistart.logger.Logger;
import com.aistart.pojo.Student;
import com.aistart.service.StudentService;/*** 代理类,里面的函数理应与目标类(被代理类,核心类)一致*/
public class StudentServiceProxy implements StudentService {/*目标类*/private StudentService target;/*增强类*/private Logger logger;public StudentServiceProxy(StudentService target, Logger logger) {this.target = target;this.logger = logger;}@Overridepublic boolean addStu() {/*击打日志*/logger.LogInfoBefore();boolean b = target.addStu();//通过当前代理类访问目标类的logger.LogInfoAfter();return false;}@Overridepublic boolean delStu() {logger.LogInfoBefore();boolean b = target.delStu();logger.LogInfoAfter();return false;}
}

StudentServiceProxy1.java

package com.aistart.proxy;import com.aistart.logger.Logger;
import com.aistart.service.StudentService;
import com.aistart.service.impl.StudentServiceImpl;/*** 代理类,里面的函数理应与目标类(被代理类,核心类)一致*/
public class StudentServiceProxy1 extends StudentServiceImpl {/*目标类*/private StudentService target;/*增强类*/private Logger logger;public StudentServiceProxy1(StudentService target, Logger logger) {this.target = target;this.logger = logger;}@Overridepublic boolean addStu() {/*击打日志*/logger.LogInfoBefore();boolean b = target.addStu();//通过当前代理类访问目标类的logger.LogInfoAfter();return false;}@Overridepublic boolean delStu() {logger.LogInfoBefore();boolean b = target.delStu();logger.LogInfoAfter();return false;}
}

StudentServiceProxyTest.java

package com.aistart.proxy;import com.aistart.logger.Logger;
import com.aistart.service.StudentService;
import com.aistart.service.impl.StudentServiceImpl;
import org.junit.Test;import java.lang.reflect.Proxy;import static org.junit.Assert.*;public class StudentServiceProxyTest {@Testpublic void addStu() {/*去测试,也就是直接访问,代理对象而不是访问目标*//*获取当前的目标对象*/StudentServiceImpl target = new StudentServiceImpl();/*获取增强的类*/Logger logger = new Logger();StudentService studentServiceProxy = new StudentServiceProxy(target, logger);studentServiceProxy.addStu();}}
http://www.lryc.cn/news/335152.html

相关文章:

  • 基于JAX的二阶优化方法的实践
  • 【计算机考研】408算法大题怎么练?
  • 输入框验证数字类型
  • LeetCode 377——组合总和 Ⅳ
  • ubuntu同步网络时间
  • Flink学习(四)-数据管道 ETL
  • Python可视化之Matplotlib
  • ChatGPT全方位解析:如何培养 AI 智能对话技能?
  • [C++/Linux] UDP编程
  • 深入探索Linux的lsof命令
  • flowable 想改变正在运行的任务,实例版本为最新,需要改哪些表
  • 统计各位数字都不同的数字个数 II
  • Taro框架中的H5 模板基本搭建
  • gitea详细介绍
  • 应用性能分析系统SkyWalking的安装及使用详解
  • 服务器远程桌面连接不上怎么办?
  • C++之STL的algorithm(8)之适配器(bind等)整理
  • 部分国企笔试总结
  • 《QT实用小工具·二十二》多种样式导航按钮控件
  • 不定长顺序表
  • 5.网络编程-socker(golang版)
  • 网格矢量如何计算莫兰指数
  • 《containerd原理剖析与实战》大模型时代下如何学习云原生
  • 【实用工具】使用飞书机器人监控工程日志
  • NIKKE胜利女神PC怎么设置中文 手把手教你设置中文教程
  • 【leetcode面试经典150题】2.移除元素(C++)
  • 实现几何对象按照一定距离向外缓冲
  • 现代深度学习模型和技术
  • go的orm框架-Gorm
  • 嵌入式开发学习---(部分)数据结构(无代码)