Spring的aop及动态代理

 1.1.1 什么是AOP

AOP:全称是Aspect Oriented Programming即:面向切面编程。

简单的说它就是把我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上,对我们的已有方法进行增强。

例如,下面这段代码就可以使用AOP改造。

 1.1.2 AOP的作用及优势

  作用: 在程序运行期间,不修改源码对已有方法进行增强。

  优势: 减少重复代码 提高开发效率 维护方便

AOP的实现方式 使用动态代理技术

1.2.4 动态代理回顾

1.2.4.1 动态代理的特点

字节码随用随创建,随用随加载。 它与静态代理的区别也在于此。因为静态代理是字节码一上来就创建好,并完成加载。 装饰者模式就是静态代理的一种体现。

1.2.4.2 动态代理常用的有两种方式

基于接口的动态代理 提供者:JDK官方的Proxy类。 要求:被代理类最少实现一个接口。

基于子类的动态代理 提供者:第三方的CGLib,如果报asmxxxx异常,需要导入asm.jar。 要求:被代理类不能用final修饰的类(最终类)。

1.2.4.3 使用JDK官方的Proxy类创建代理对象  

创建ProxyProducer接口,代码如下:

public interface ProxyProducer {

    /**
     * 销售商品
     * @param money
     */
    public void saleProduct(Float money);

    /**
     * 售后服务
     * @param money
     */
    public void afterService(Float money) ;
}

 创建Producer类实现ProxyProducer接口,代码如下:

public class Producer implements ProxyProducer {

    /**
     * 销售商品
     * @param money
     */
    public void saleProduct(Float money) {
        System.out.println("销售商品,金额是:"+money);
    }

    /**
     * 售后服务
     * @param money
     */
    public void afterService(Float money) {
        System.out.println("提供售后服务,金额是:"+money);
    }
}

 创建JDK动态代理,代码如下:

 

public class Consumer {

    public static void main(String[] args) {
        //创建对象实例
        Producer producer = new Producer();
        producer.saleProduct(5000f);
        producer.afterService(1000f);

        /***
         * 基于动态代理改造
         * 动态代理:
         *      特点:字节码随用随创建,随用随加载
         *      分类:基于接口的动态代理,基于子类的动态代理
         *      作用:不修改源码的基础上对方法增强
         *      基于接口的动态代理:
         *          提供者是:JDK官方
         *          使用要求:被代理类最少实现一个接口。
         *          涉及的类:Proxy
         *          创建代理对象的方法:newProxyInstance
         *          方法的参数:
         *              ClassLoader:类加载器。用于加载代理对象的字节码。和被代理对象使用相同的类加载器。固定写法。
         *              Class[]:字节码数组。用于给代理对象提供方法。和被代理对象具有相同的方法。和被代理对象实现相同的接口,就会具有相同的方法。固定写法
         *              InvocationHanlder:要增强的方法。此处是一个接口,我们需要提供它的实现类。通常写的是匿名内部类。
         *              增强的代码随用随写。
         */
        ProxyProducer proxyProducer = (ProxyProducer) Proxy.newProxyInstance(
                Producer.class.getClassLoader(),
                Producer.class.getInterfaces(),
                new InvocationHandler() {
                    /**
                     * 执行被代理对象的任何方法都都会经过该方法,该方法有拦截的作用
                     * 参数的含义
                     * 	Object proxy:代理对象的引用。一般不用
                     *  Method method:当前执行的方法
                     *  Object[] args:当前方法所需的参数
                     * 返回值的含义
                     * 	和被代理对象的方法有相同的返回值
                     */
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        //方法参数
                        Float money = (Float) args[0];

                        //获取方法名字
                        String methodName = method.getName();

                        //定义一个对象,存储返回结果
                        Object result = null;

                        //销售提成25%
                        if(methodName.equals("saleProduct")){
                            result= method.invoke(producer,money*0.75f);
                        }else if(methodName.equals("saleProduct")){
                            //销售提成10%
                            result= method.invoke(producer,money*0.9f);
                        }
                        return result;
                    }
                }
        );

        proxyProducer.saleProduct(1000f);
        proxyProducer.afterService(1000f);
    }
}

1.2.4.4 使用CGLib的Enhancer类创建代理对象

还是那个例子,只不过不让他实现接口,此时不能使用JDK的动态代理了,我们可以使用CGLib动态代理,首先我们需要引入依赖。  

<dependencies>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.1_3</version>
    </dependency>
</dependencies>

 修改Producer,不然它实现任何接口,代码如下:

public class Producer {

    /**
     * 销售商品
     * @param money
     */
    public void saleProduct(Float money) {
        System.out.println("销售商品,金额是:"+money);
    }

    /**
     * 售后服务
     * @param money
     */
    public void afterService(Float money) {
        System.out.println("提供售后服务,金额是:"+money);
    }
}

 创建Consumer,使用CGLib动态代理,代码如下:

public class Consumer {

    public static void main(String[] args) {
        //创建对象实例
        Producer producer = new Producer();
        producer.saleProduct(5000f);
        producer.afterService(1000f);


        /**
         * 动态代理:
         * 	 特点:字节码随用随创建,随用随加载
         * 	 分类:基于接口的动态代理,基于子类的动态代理
         * 	 作用:不修改源码的基础上对方法增强
         * 	 基于子类的动态代理
         * 		提供者是:第三方cglib包,在使用时需要先导包(maven工程导入坐标即可)
         * 		使用要求:被代理类不能是最终类,不能被final修饰
         * 		涉及的类:Enhancer
         * 		创建代理对象的方法:create
         * 		方法的参数:
         * 			Class:字节码。被代理对象的字节码。可以创建被代理对象的子类,还可以获取被代理对象的类加载器。
         * 			Callback:增强的代码。通常都是写一个接口的实现类或者匿名内部类。
         * 					 我们在使用时一般都是使用Callback接口的子接口:MethodInterceptor
         */
        Producer proxyProducer = (Producer) Enhancer.create(producer.getClass(), new MethodInterceptor() {
            @Override
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                //方法参数
                Float money = (Float) args[0];

                //获取方法名字
                String methodName = method.getName();

                //定义一个对象,存储返回结果
                Object result = null;

                //销售提成25%
                if(methodName.equals("saleProduct")){
                    result= method.invoke(producer,money*0.75f);
                }else if(methodName.equals("saleProduct")){
                    //销售提成10%
                    result= method.invoke(producer,money*0.9f);
                }
                return result;
            }
        });

        proxyProducer.saleProduct(1000f);
        proxyProducer.afterService(1000f);
    }
}

 2.1 Spring中AOP的细节

2.1.2 AOP相关术语

Joinpoint(连接点):
        所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点。
Pointcut(切入点):
        所谓切入点是指我们要对哪些Joinpoint进行拦截的定义。
Advice(通知/增强):
        所谓通知是指拦截到Joinpoint之后所要做的事情就是通知。
        通知的类型:前置通知,后置通知,异常通知,最终通知,环绕通知。
Introduction(引介):
        引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field。
Target(目标对象):
        代理的目标对象。
Weaving(织入):
        是指把增强应用到目标对象来创建新的代理对象的过程。
        spring采用动态代理织入,而AspectJ采用编译期织入和类装载期织入。
Proxy(代理):
        一个类被AOP织入增强后,就产生一个结果代理类。
Aspect(切面):
        是切入点和通知(引介)的结合。 

 2.1.3 学习spring中的AOP要明确的事

a、开发阶段(我们做的)
    编写核心业务代码(开发主线):大部分程序员来做,要求熟悉业务需求。
    把公用代码抽取出来,制作成通知。(开发阶段最后再做):AOP编程人员来做。
    在配置文件中,声明切入点与通知间的关系,即切面。:AOP编程人员来做。
b、运行阶段(Spring框架完成的)
    Spring框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。 

2.2 基于XML的AOP配置  

示例:
    我们在学习spring的aop时,采用输出日志作为示例。
    在业务层方法执行的前后,加入日志的输出。
    并且把spring的ioc也一起应用进来。 

 2.2.1 第一步:创建maven工程并导入坐标

<dependencies>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-context</artifactId>
  		<version>5.0.2.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>org.aspectj</groupId>
  		<artifactId>aspectjweaver</artifactId>
  		<version>1.8.7</version>
  	</dependency>
</dependencies>

 2.2.2 第二步:准备必要的代码

public interface AccountService {
	
	/**
	 * 模拟保存
	 */
	void saveAccount();
	
	/**
	 * 模拟更新
	 * @param i
	 */
	void updateAccount(int i);
	
	/**
	 * 模拟删除
	 * @return
	 */
	int deleteAccount();
}

public class AccountServiceImpl implements AccountService {

	@Override
	public void saveAccount() {
		System.out.println("保存了账户");
	}

	@Override
	public void updateAccount(int i) {
		System.out.println("更新了账户"+i);
	}

	@Override
	public int deleteAccount() {
		System.out.println("删除了账户");
		return 0;
	}
}

/**
 * 模拟一个用于记录日志的工具类
 */
public class Logger {

	/**
	 * 用于打印日志
	 * 计划让其在切入点方法执行之前执行
	 */
	public void printLog() {
		System.out.println("Logger类中的printLog方法开始记录日志了。。。");
	}
}

2.2.3 第三步:创建spring的配置文件并导入约束

 


<?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: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/aop 
       			   http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

 2.2.4第四步:配置spring的ioc

<!-- 配置service -->
<bean id="accountService" class="com.zt.service.impl.AccountServiceImpl"></bean>

 2.2.5 第五步:配置aop

<!-- 
aop的配置步骤:
	第一步:把通知类的创建也交给spring来管理
	第二步:使用aop:config标签开始aop的配置
	第三步:使用aop:aspect标签开始配置切面,写在aop:config标签内部
			id属性:给切面提供一个唯一标识
			ref属性:用于引用通知bean的id。
	第四步:使用对应的标签在aop:aspect标签内部配置通知的类型
			使用aop:befored标签配置前置通知,写在aop:aspect标签内部
				method属性:用于指定通知类中哪个方法是前置通知
				pointcut属性:用于指定切入点表达式。
			切入点表达式写法:
				关键字:execution(表达式)
			表达式内容:
				全匹配标准写法:
					访问修饰符   返回值   包名.包名.包名...类名.方法名(参数列表)
				例如:
				public void com.itheima.service.impl.AccountServiceImpl.saveAccount()
-->
	
<!-- 配置通知类 -->
<bean id="logger" class="com.zt.utils.Logger"></bean>	
<!-- 配置aop -->
<aop:config>
	<!-- 配置切面 -->
	<aop:aspect id="logAdvice" ref="logger">
		<!-- 配置前置通知 -->
		<aop:before method="printLog" pointcut="execution( * com.zt.service.impl.*.*(..))"/>
	</aop:aspect>
</aop:config>

2.2.6切入点表达式说明

execution:匹配方法的执行(常用)
总结:*代表任意方法,类,包,返回值,..任意包和它的子包或者任意参数
    execution(表达式)
表达式语法:execution([修饰符] 返回值类型 包名.类名.方法名(参数))
写法说明:
    全匹配方式:
        public void com.itheima.service.impl.AccountServiceImpl.saveAccount(com.itheima.domain.Account)
    访问修饰符可以省略    
        void com.itheima.service.impl.AccountServiceImpl.saveAccount(com.itheima.domain.Account)
    返回值可以使用*号,表示任意返回值
        * com.itheima.service.impl.AccountServiceImpl.saveAccount(com.itheima.domain.Account)
    包名可以使用*号,表示任意包,但是有几级包,需要写几个*
        * *.*.*.*.AccountServiceImpl.saveAccount(com.itheima.domain.Account)
    使用..来表示当前包,及其子包
        * com..AccountServiceImpl.saveAccount(com.itheima.domain.Account)
    类名可以使用*号,表示任意类
        * com..*.saveAccount(com.itheima.domain.Account)
    方法名可以使用*号,表示任意方法
        * com..*.*( com.itheima.domain.Account)
    参数列表可以使用*,表示参数可以是任意数据类型,但是必须有参数
        * com..*.*(*)
    参数列表可以使用..表示有无参数均可,有参数可以是任意类型
        * com..*.*(..)
    全通配方式:
        * *..*.*(..)
注:
    通常情况下,我们都是对业务层的方法进行增强,所以切入点表达式都是切到业务层实现类。
    execution(* com.itheima.service.impl.*.*(..))

2.2.7 aop:pointcut  

aop:pointcut:
    作用:
        用于配置切入点表达式。就是指定对哪些类的哪些方法进行增强。
    属性:
        expression:用于定义切入点表达式。
        id:用于给切入点表达式提供一个唯一标识
<aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/>

2.2.8 通知的四种常用类型  

aop:before
	作用:
		用于配置前置通知。指定增强的方法在切入点方法之前执行 
	属性:
		method:用于指定通知类中的增强方法名称
		ponitcut-ref:用于指定切入点的表达式的引用
		poinitcut:用于指定切入点表达式	
	执行时间点:
		切入点方法执行之前执行
<aop:before method="beginPrintLog" pointcut-ref="pt1"/>

aop:after-returning
	作用:
		用于配置后置通知
	属性:
		method:指定通知中方法的名称。
		pointct:定义切入点表达式
		pointcut-ref:指定切入点表达式的引用
	执行时间点:
		切入点方法正常执行之后。它和异常通知只能有一个执行
<aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"/>

aop:after-throwing
	作用:
		用于配置异常通知
	属性:
		method:指定通知中方法的名称。
		pointct:定义切入点表达式
		pointcut-ref:指定切入点表达式的引用
	执行时间点:
		切入点方法执行产生异常后执行。它和后置通知只能执行一个
<aop:after-throwing method="afterThrowingPringLog" pointcut-ref="pt1"/>
			
aop:after
	作用:
		用于配置最终通知
	属性:
		method:指定通知中方法的名称。
		pointct:定义切入点表达式
		pointcut-ref:指定切入点表达式的引用
	执行时间点:
		无论切入点方法执行时是否有异常,它都会在其后面执行。
<aop:after method="afterPringLog" pointcut-ref="pt1"/>

 2.2.9 环绕通知

配置方式:
<aop:config>
	<aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/>	
	<aop:aspect id="txAdvice" ref="txManager">
		<!-- 配置环绕通知 -->
		<aop:around method="transactionAround" pointcut-ref="pt1"/>
	</aop:aspect>
</aop:config>

aop:around:
	作用:
		用于配置环绕通知
	属性:
		method:指定通知中方法的名称。
		pointct:定义切入点表达式
		pointcut-ref:指定切入点表达式的引用
	说明:
		它是spring框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式。
	注意:
		通常情况下,环绕通知都是独立使用的
	/**
	 * 环绕通知
	 * 问题:
	 * 	当配置完环绕通知之后,没有业务层方法执行(切入点方法执行)
	 * 分析:
	 *  通过动态代理的代码分析,我们现在的环绕通知没有明确的切入点方法调用
	 * 解决:
	 * 	spring框架为我们提供了一个接口,该接口可以作为环绕通知的方法参数来使用
	 * 	ProceedingJoinPoint。当环绕通知执行时,spring框架会为我们注入该接口的实现类。
	 *  它有一个方法proceed(),就相当于invoke,明确的业务层方法调用
	 *  
	 *  spring的环绕通知:
	 *  	它是spring为我们提供的一种可以在代码中手动控制增强方法何时执行的方式。
	 */
	public void aroundPrintLog(ProceedingJoinPoint pjp) {
		
		try {
			System.out.println("前置Logger类中的aroundPrintLog方法开始记录日志了");
			pjp.proceed();//明确的方法调用
			System.out.println("后置Logger类中的aroundPrintLog方法开始记录日志了");
		} catch (Throwable e) {
			System.out.println("异常Logger类中的aroundPrintLog方法开始记录日志了");
			e.printStackTrace();
		}finally {
			System.out.println("最终Logger类中的aroundPrintLog方法开始记录日志了");
		}
	}

2.3 基于注解的AOP配置

2.3.1 第一步:导入maven工程的依赖坐标和必要的代码 拷贝上一小节的工程即可。  

2.3.2 第二步:在配置文件中导入context的名称空间  

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
   
   <!-- 配置spring创建容器时要扫描的包 -->
   <context:component-scan base-package="com.zt"></context:component-scan>
</beans>

 2.3.3 第三步:把资源使用注解配置

@Service("accountService")
public class AccountServiceImpl implements AccountService {

	@Override
	public void saveAccount() {
		System.out.println("保存了账户");
		int i=1/0;
	}

	@Override
	public void updateAccount(int i) {
		System.out.println("更新了账户"+i);
	}

	@Override
	public int deleteAccount() {
		System.out.println("删除了账户");
		return 0;
	}
} 

 2.3.4 第四步:把通知类也使用注解配置

@Component("logger")
public class Logger {
}

 2.3.5 第五步:在通知类上使用@Aspect注解声明为切面

作用:
	把当前类声明为切面类。

/**
 * 模拟一个用于记录日志的工具类
 * @Version 1.0
 */
@Component("logger")
@Aspect//表示当前类是一个切面类
public class Logger {}

 2.3.6 第六步:使用注解配置通知类型

@Before
	作用:
		把当前方法看成是前置通知。
	属性:
		value:用于指定切入点表达式,还可以指定切入点表达式的引用。
	
	/**
	 * 前置通知
	 */
	@Before("execution(* com.zt.service.impl.*.*(..))")
	public void beforePrintLog() {
		System.out.println("前置通知:Logger类中的beforePrintLog方法开始记录日志了。。。");
	}

	@AfterReturning
	作用:
		把当前方法看成是后置通知。
	属性:
		value:用于指定切入点表达式,还可以指定切入点表达式的引用

	/**
	 * 后置通知
	 */
	@AfterReturning("execution(* com.zt.service.impl.*.*(..))")
	public void afterReturningPrintLog() {
		System.out.println("后置通知:Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
	}

	@AfterThrowing
	作用:
		把当前方法看成是异常通知。
	属性:
			value:用于指定切入点表达式,还可以指定切入点表达式的引用
	
	/**
	 * 异常通知
	 */
	@AfterThrowing("execution(* com.zt.service.impl.*.*(..))")
	public void afterThrowingPrintLog() {
		System.out.println("异常通知:Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
	}

@After
	作用:
		把当前方法看成是最终通知。
	属性:
		value:用于指定切入点表达式,还可以指定切入点表达式的引用
	
	/**
	 * 最终通知
	 */
	@After("execution(* com.zt.service.impl.*.*(..))")
	public void afterPrintLog() {
		System.out.println("最终通知:Logger类中的afterPrintLog方法开始记录日志了。。。");
	}

 2.3.7 第四步:在spring配置文件中开启spring对注解AOP的支持

<!-- 开启spring对注解AOP的支持 -->
<aop:aspectj-autoproxy/>

2.3.8 环绕通知注解配置  

@Around
作用:
	把当前方法看成是环绕通知。
属性:
	value:用于指定切入点表达式,还可以指定切入点表达式的引用。

	/**
	 * 环绕通知
	 * 问题:
	 * 	当配置完环绕通知之后,没有业务层方法执行(切入点方法执行)
	 * 分析:
	 *  通过动态代理的代码分析,我们现在的环绕通知没有明确的切入点方法调用
	 * 解决:
	 * 	spring框架为我们提供了一个接口,该接口可以作为环绕通知的方法参数来使用
	 * 	ProceedingJoinPoint。当环绕通知执行时,spring框架会为我们注入该接口的实现类。
	 *  它有一个方法proceed(),就相当于invoke,明确的业务层方法调用
	 *  
	 *  spring的环绕通知:
	 *  	它是spring为我们提供的一种可以在代码中手动控制增强方法何时执行的方式。
	 */
	@Around("execution(* com.zt.service.impl.*.*(..))")
	public void aroundPrintLog(ProceedingJoinPoint pjp) {
		
		try {
			System.out.println("前置Logger类中的aroundPrintLog方法开始记录日志了");
			pjp.proceed();//明确的方法调用
			System.out.println("后置Logger类中的aroundPrintLog方法开始记录日志了");
		} catch (Throwable e) {
			System.out.println("异常Logger类中的aroundPrintLog方法开始记录日志了");
			e.printStackTrace();
		}finally {
			System.out.println("最终Logger类中的aroundPrintLog方法开始记录日志了");
		}
	}	

 2.3.9 切入点表达式注解

@Pointcut
作用:
	指定切入点表达式
属性:
	value:指定表达式的内容

@Pointcut("execution(* com.zt.service.impl.*.*(..))")
private void pt1() {}

引用方式:
	/**
	 * 环绕通知
	 * @param pjp
	 * @return
	 */
	@Around("pt1()")//注意:千万别忘了写括号
public void aroundPrintLog(ProceedingJoinPoint pjp) {
}

2.3.10 不使用XML的配置方式  

 

@Configuration
@ComponentScan(basePackages="com.zt")
@EnableAspectJAutoProxy //开启aop切面
public class SpringConfiguration {
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值