代理模式

静态代理

直接看例子

  1. package com.lyd.demo01;
  2. public class Host implements Rent{
  3. @Override
  4. public void rent() {
  5. System.out.println("房东出租房子");
  6. }
  7. }
  1. package com.lyd.demo01;
  2. public class Proxy implements Rent{
  3. Host host;
  4. public Proxy() {
  5. }
  6. public Proxy(Host host) {
  7. this.host = host;
  8. }
  9. @Override
  10. public void rent() {
  11. //经过一些步骤才能租房
  12. seeHouse();
  13. hetong();
  14. fare();
  15. host.rent();
  16. }
  17. /*
  18. 中介能做房东不能做的事
  19. * */
  20. public void seeHouse(){
  21. System.out.println("中介带你看房");
  22. }
  23. public void hetong(){
  24. System.out.println("签租赁合同");
  25. }
  26. public void fare(){
  27. System.out.println("收中介费");
  28. }
  29. }
  1. package com.lyd.demo01;
  2. public class Client {
  3. public static void main(String[] args) {
  4. /*
  5. 不去直接找房东租房,而是找代理代理租房,代理去找房东租房
  6. 至于代理怎么租的房,我们不用管,只要找代理就行了,不用直接接触房东。
  7. 为什么不直接找房东而要找中介呢。因为中介在租房时要做一些格外的事,是房东做不了的。
  8. 别看中介租房和房东租房都是租房,中介租房多做了一些事。
  9. 在中介这里可以横向的拓展更多的事,收中介费啊,看房啊,为什么这些事不在房东这里拓展非要去中介这里拓展呢
  10. 抽象的看,房东相当于源码,你去房东这里拓展,不就是相当于改源码吗,中介相当于加一层,调用房东的同时,做格外的事,如果出错了也方便排错
  11. 这不就像是三层架构吗,dao层处理数据,要拓展一些新功能,在到service层代理dao层,横向发展
  12. * */
  13. Host host =new Host();
  14. Proxy proxy =new Proxy(host);
  15. proxy.rent();
  16. }
  17. }

动态代理

接上面租房的例子。
主要使用InvocationHandler接口和Proxy类。
动态代理就是靠反射自动生成一个代理,不用在一个一个写代理了。

  1. package com.lyd.demo01;
  2. import com.sun.org.apache.xml.internal.security.keys.storage.implementations.CertsInFilesystemDirectoryResolver;
  3. import java.lang.reflect.InvocationHandler;
  4. import java.lang.reflect.Method;
  5. import java.lang.reflect.Proxy;
  6. public class ProxyInvocationHandler implements InvocationHandler {
  7. private Object target;//被代理的东西
  8. public void setTarget(Object target){
  9. this.target=target;
  10. }
  11. //生成代理类
  12. public Object getProxy(){
  13. return Proxy.newProxyInstance(this.getClass().getClassLoader(),
  14. target.getClass().getInterfaces(),
  15. this);
  16. }
  17. //处理代理实例,并返回结果
  18. @Override
  19. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  20. //需要拓展什么业务时就可以在直接加
  21. A();
  22. return method.invoke(target,args);
  23. }
  24. public void A(){
  25. System.out.println("执行了A方法");
  26. }
  27. }
  1. package com.lyd.demo01;
  2. public class Client {
  3. public static void main(String[] args) {
  4. Host host =new Host();
  5. ProxyInvocationHandler pih =new ProxyInvocationHandler();
  6. pih.setTarget(host);
  7. //动态代理的是接口
  8. Rent proxy =(Rent) pih.getProxy();
  9. proxy.rent();
  10. /*执行结果:
  11. * 执行了A方法
  12. * 房东出租房子
  13. * */
  14. }
  15. }

AOP模式图

需要拓展新功能的时候,横向拓展,代理底层的源码,附加新功能
image.png

Spring实现AOP

使用AOP需要导入一个依赖包

  1. <dependency>
  2. <groupId>org.aspectj</groupId>
  3. <artifactId>aspectjweaver</artifactId>
  4. <version>1.9.1</version>
  5. <scope>runtime</scope>
  6. </dependency>

使用Spring的API接口

设置一个接口和实现类

  1. public interface IUserService {
  2. public void add();
  3. public void delete();
  4. public void update();
  5. public void select();
  6. }
  7. public class UserService implements IUserService{
  8. @Override
  9. public void add() {
  10. System.out.println("增加了一个用户");
  11. }
  12. @Override
  13. public void delete() {
  14. System.out.println("删除了一个用户");
  15. }
  16. @Override
  17. public void update() {
  18. System.out.println("修改了一个用户");
  19. }
  20. @Override
  21. public void select() {
  22. System.out.println("查询了一个用户");
  23. }
  24. }

我们有一个需求,在增删改查方法前后都要加上一个日志功能。
如果在上面的add,delete,update,select方法里面都加上前置日志和后置日志,很麻烦。

  1. package com.lyd.log;
  2. import org.springframework.aop.MethodBeforeAdvice;
  3. import java.lang.reflect.Method;
  4. //前置日志,看接口名字就知道是方法执行之前执行
  5. public class Log implements MethodBeforeAdvice {
  6. //method:要执行的目标对象方法
  7. //args:参数
  8. //target:目标对象
  9. @Override
  10. public void before(Method method, Object[] args, Object target) throws Throwable {
  11. System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
  12. }
  13. }
  1. package com.lyd.log;
  2. import org.springframework.aop.AfterReturningAdvice;
  3. import java.lang.reflect.Method;
  4. //后置日志,看接口名字就知道是方法执行之后执行
  5. public class AfterLog implements AfterReturningAdvice {
  6. //returnValue:返回值
  7. @Override
  8. public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
  9. System.out.println("执行了"+method.getName()+"返回结果为"+returnValue);
  10. }
  11. }

设置配置文件,注入上面的xml名称空间

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. https://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop
  8. https://www.springframework.org/schema/aop/spring-aop.xsd">
  9. <!--注册bean-->
  10. <bean id="userService" class="com.lyd.service.UserService"/>
  11. <bean id="log" class="com.lyd.log.Log"/>
  12. <bean id="afterLog" class="com.lyd.log.AfterLog"/>
  13. <!--使用原生的Spring接口-->
  14. <!--配置AOP:需要导入AOP的约束-->
  15. <aop:config>
  16. <!--切入点:expression:表达式,execution(要执行的位置)-->
  17. <aop:pointcut id="pointcut" expression="execution(* com.lyd.service.UserService.*(..))"/>
  18. <!--执行环绕增加-->
  19. <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
  20. <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
  21. </aop:config>
  22. </beans>

执行

  1. public static void main(String[] args) {
  2. ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
  3. //动态代理的是接口
  4. IUserService user = context.getBean("userService", IUserService.class);
  5. user.add();
  6. }

image.png

使用自定义类实现AOP

接上面增删改查的例子
写一个新的自定义类,两个方法一前一后的执行

  1. package com.lyd.diy;
  2. /**
  3. * @author liyadong
  4. * @create 2022-04-01-14:57
  5. */
  6. public class DiyPointCut {
  7. public void before(){
  8. System.out.println("-------方法执行前-------");
  9. }
  10. public void after(){
  11. System.out.println("=========方法执行后==========");
  12. }
  13. }

修改配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. https://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop
  8. https://www.springframework.org/schema/aop/spring-aop.xsd">
  9. <!--注册bean-->
  10. <bean id="userService" class="com.lyd.service.UserService"/>
  11. <bean id="diy" class="com.lyd.diy.DiyPointCut"/>
  12. <aop:config>
  13. <!--自定义的切面,ref要引用的类-->
  14. <aop:aspect ref="diy">
  15. <!--切入点,就是要代理哪个方法-->
  16. <aop:pointcut id="point" expression="execution(* com.lyd.service.UserService.*(..))"/>
  17. <!--
  18. 通知
  19. 在哪里执行什么方法
  20. aop:before:在切入点之前执行
  21. aop:after:在切入点之后执行
  22. method:要执行的方法,这个方法就是diy里面的
  23. pointcut-ref:切入点
  24. -->
  25. <aop:before method="before" pointcut-ref="point"/>
  26. <aop:after method="after" pointcut-ref="point"/>
  27. </aop:aspect>
  28. </aop:config>
  29. </beans>

执行
代码和上面的一样
image.png

注解实现AOP

设置配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. https://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop
  8. https://www.springframework.org/schema/aop/spring-aop.xsd">
  9. <!--注册bean-->
  10. <bean id="userService" class="com.lyd.service.UserService"/>
  11. <bean id="annotationPointCut" class="com.lyd.diy.AnnotationPointCut"/>
  12. <!--开启注解支持-->
  13. <aop:aspectj-autoproxy/>
  14. </beans>

添加一个自定义注解实现类

  1. package com.lyd.diy;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.After;
  4. import org.aspectj.lang.annotation.Around;
  5. import org.aspectj.lang.annotation.Aspect;
  6. import org.aspectj.lang.annotation.Before;
  7. //使用注解的方式实现AOP
  8. @Aspect //标记这个类是一个切面
  9. public class AnnotationPointCut {
  10. @Before("execution(* com.lyd.service.UserService.*(..))")
  11. public void before(){
  12. System.out.println("-------Ann方法执行前-------");
  13. }
  14. @After("execution(* com.lyd.service.UserService.*(..))")
  15. public void after(){
  16. System.out.println("=========Ann方法执行后==========");
  17. }
  18. //在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
  19. @Around("execution(* com.lyd.service.UserService.*(..))")
  20. public void around(ProceedingJoinPoint jp) throws Throwable {
  21. System.out.println("++++++++++++Ann环绕前++++++++");
  22. //执行方法
  23. Object object = jp.proceed();
  24. System.out.println("++++++++++++Ann环绕后++++++++");
  25. }
  26. }

运行代码和上面一样的
image.png