Spring框架总结(3)

AOP&代理模式

  1. AOP:面向切面编程
    1. 将分散在各个方法中的相同的功能(日志处理,权限控制)提取出来,并在运行时动态的将切面注入到各方法中;
    2. 它是OOP的有力补充,OOP是纵向的抽象,AOP是横向的抽象
    3. 业务代码变得简洁
    4. 便于维护
  2. 代理模式
    1. 解决的问题:无法直接访问某个目标时(没有权限;找不到目标对象),可以使用代理模式
    2. 与装饰模式相似,但解决的问题(初衷)不一样
      1. 题外话:装饰模式
        1. 给一个已知的类动态的增加一个功能,这个类能直接访问;按照以前的方式,我们可以创建一个子类就可以解决了,但是这样做会使类的数量急剧膨胀
        2. 最典型的用法:流(流量),一个流没有这个功能,我们可以再套一层流,从而增加了一个功能;前提是这个流是可以访问到的
    3. 三个角色:
      1. 抽象角色:
      2. 真实角色
      3. 代理角色
      4. 三者关系(类图)
      5. 存在的问题:每一个真实角色都得有一个代理角色与之对应
      6. 解决的方法:动态代理
    4. 动态代理
      1. 为每个真实对象动态生成一个与之匹配的代理对象
      2. 相同接口
        1. Proxy类:根据接口,真实对象和调用处理器来生成代理对象
        2. InvocationHandler接口:调用处理器(重难点)
      3. JDK动态代理的缺点:
        1. 必须提供一个接口
        2. 若不想使用接口,可使用cglib通过创建子类实现动态代理(Spring两者都用)
  3. AOP的术语
    1. 增强(advice):也叫通知;新增的功能(代码段)
    2. 连接点(Joinpoint):程序执行的某个特定的位置;Spring只支持方法(method)
    3. 切点(pointcut):具体的连接点;通常用一个表达式表示切点
    4. 目标对象(Target)
    5. 织入(weaving):将增强添加到目标对象切点的过程
      1. 编译期
      2. 加载期
      3. 运行期
    6. 代理对象(Proxy):向目标对象中加入增强后产生的新对象
    7. 切面(Aspect):增强 + 切点
  4. AOP的提供者
    1. AspectJ:开源的AOP框架,支持在三个时期加入增强
    2. SpringAOP:Spring自带的AOP,在运行期加入增强(JDB动态代理,Cglib)
    3. JBossAOP
  5. AspectJ的实现
    1. 加入aspect依赖包
    2. 加入<aop:aspectj-autoproxy>
    3. 创建切面 @Component,@Aspect
      1. 增强(通知):@Before,@After,@Around,@AfterReturning,@AfterThrowing
      2. 切点:execution(* com.oracle.proxy.*.*(..)) [ 详见下面代码 ]
    4. 大量繁琐代码来袭(注意:test 中创建houser的方法有变化 [ houser上转型为接口对象 ],因为JDK动态代理必须提供接口的限制)
      1. applicationContext.xml
        
        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        	xmlns:p="http://www.springframework.org/schema/p"
        	xmlns:util="http://www.springframework.org/schema/util"
        	xmlns:context="http://www.springframework.org/schema/context"
        	xmlns:aop="http://www.springframework.org/schema/aop"
        	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
        
        
        	<context:component-scan base-package="com.oracle.proxy"></context:component-scan>
        	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
        	
        </beans>
        
        -----------------------------------------------------------------------------------------
        
        Houser.java
        
        package com.oracle.proxy;
        
        import org.springframework.stereotype.Component;
        
        @Component
        public class Houser implements Rentable{
        	@Override
        	public void rent(int money) {
        		System.out.println("我是房东,收取房费:"+money);
        	}
        
        	@Override
        	public int sum(int a) {
        		return (1+a)*a/2;
        	}
        }
        
        -----------------------------------------------------------------------------------------
        
        LogAspect.java
        
        package com.oracle.proxy;
        
        
        import java.util.Arrays;
        
        import org.aspectj.lang.JoinPoint;
        import org.aspectj.lang.annotation.After;
        import org.aspectj.lang.annotation.Aspect;
        import org.aspectj.lang.annotation.Before;
        import org.springframework.stereotype.Component;
        
        @Component
        @Aspect
        public class LogAspect {
        	
        	@Before("execution(* com.oracle.proxy.*.rent(..))")
        	public void before(JoinPoint point) {
        		System.out.println("记录日志开始");
        		System.out.println("目标对象:"+point.getTarget());
        		System.out.println("参数:"+Arrays.toString(point.getArgs()));
        		System.out.println("方法名:"+point.getSignature().getName());		
        	}
        	
        	@After("execution(* com.oracle.proxy.*.rent(..))")
        	public void after() {
        		System.out.println("记录日志结束");
        	}
        }
        
        -----------------------------------------------------------------------------------------
        
        Rentable.java
        
        package com.oracle.proxy;
        
        public interface Rentable {
        
        	public void rent(int money);
        	public int sum(int a);
        }
        
        -----------------------------------------------------------------------------------------
        
        Test.java
        
        package com.oracle.test;
        
        import org.junit.After;
        import org.junit.Before;
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        
        import com.oracle.proxy.Rentable;
        
        public class Test {
        
        	ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        	
        	@Before
        	public void setUp() throws Exception {
        	}
        
        	@After
        	public void tearDown() throws Exception {
        	}
        
        	@org.junit.Test
        	public void test() {
        		Rentable houser = factory.getBean(Rentable.class);
        		houser.rent(100);
        		houser.sum(100);
        	}
        
        }
        

猜你喜欢

转载自blog.csdn.net/addkai/article/details/81202332