Spring框架:Spring AOP

为什么需要AOP?

代码混乱:越来越多的非业务需求(日志和验证等)加入后, 原有的业务方法急剧膨胀.  每个方法在处理核心逻辑的同时还必须兼顾其他多个关注点.

代码分散: 以日志需求为例, 只是为了满足这个单一需求, 就不得不在多个模块(方法)里多次重复相同的日志代码. 如果日志需求发生变化, 必须修改所有模块.

问题:

普通代码:

 ArithmeticCalculator接口

package com.yorkmass.spring.aop.helloworld;

public interface ArithmeticCalculator {
	int add(int i,int j);
	int sub(int i,int j);
	int mul(int i,int j);
	int div(int i,int j);
	
}

 ArithmeticCalculatorLoggingImpl类

package com.yorkmass.spring.aop.helloworld;

public class ArithmeticCalculatorLoggingImpl implements ArithmeticCalculator {

	@Override
	public int add(int i, int j) {
		System.out.println("The method add begins with["+i+","+j+"]");
		int result=i+j;
		System.out.println("The method add ends with"+result);
		return result;
	}

	@Override
	public int sub(int i, int j) {
		System.out.println("The method sub begins with["+i+","+j+"]");
		int result=i-j;
		System.out.println("The method sub ends with"+result);
		return result;
	}

	@Override
	public int mul(int i, int j) {
		System.out.println("The method mul begins with["+i+","+j+"]");
		int result=i*j;
		System.out.println("The method mul ends with"+result);
		return result;
	}

	@Override
	public int div(int i, int j) {
		System.out.println("The method div begins with["+i+","+j+"]");
		int result=i/j;
		System.out.println("The method div ends with"+result);
		return result;
	}

}

然后自己写测试类运行即可

但是上面如果后期有需求需要对代码进行修改,则需要对每一行的日志文件进行修改,特别不方便修改。

我们能不能去掉每个方法的输入语句,只写一个,来实现对所有方法的日志管理,这样修改起来也比较方便,

动态代理便可以很好的解决这个问题。

 

使用动态代理解决问题

代理设计模式的原理: 使用一个代理将对象包装起来, 然后用该代理对象取代原始对象. 任何对原始对象的调用都要通过代理. 代理对象决定是否以及何时将方法调用转到原始对象上.

动态代理解决办法:

我们把 ArithmeticCalculatorImpl类写的尽可能简单

 ArithmeticCalculatorImpl类

package com.yorkmass.spring.aop.helloworld;

public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

	@Override
	public int add(int i, int j) {
		int result=i+j;
		return result;
	}

	@Override
	public int sub(int i, int j) {
		int result=i-j;
		return result;
	}

	@Override
	public int mul(int i, int j) {
		int result=i*j;
		return result;
	}

	@Override
	public int div(int i, int j) {
		int result=i/j;
		return result;
	}

}

我们开始配置动态代理

ArithmeticCalculatorLoggingProxy类

package com.yorkmass.spring.aop.helloworld;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

import javax.naming.spi.DirStateFactory.Result;

import org.omg.CORBA.PUBLIC_MEMBER;

public class ArithmeticCalculatorLoggingProxy {

	//要代理的对象
	private ArithmeticCalculator targer;
	public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target) {
		// TODO Auto-generated constructor stub
		this.targer=target;
	}
	public ArithmeticCalculator getLoggingProxy(){
		ArithmeticCalculator proxy=null;
		//代理对象由哪一个类加载器负责加载
		ClassLoader loader=targer.getClass().getClassLoader();
		//代理对象的类型,即其中有哪些方法
		Class[] interfaces=new Class[]{ArithmeticCalculator.class};
		//当调用代理对象其中的方法时,该执行的代码
		InvocationHandler h=new InvocationHandler() {
			/**
			 * proxy:正在返回的那个代理对象,一般情况下,在invoke方法中都不适用该对象。
			 * method:正在调用的方法
			 * args:调用方法时,传入的参数
			 */
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				String methodName=method.getName();
				//日志
				System.out.println("The method..."+methodName+"begins with"+Arrays.asList(args));
				//执行方法
				Object result=method.invoke(targer, args);
				//日志
				System.out.println("The method"+methodName+"ends with "+result);
				return result;
			}
		};

		proxy=(ArithmeticCalculator)Proxy.newProxyInstance(loader, interfaces, h);
		return proxy;
	}
}

测试主类Main:

package com.yorkmass.spring.aop.helloworld;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		ArithmeticCalculator arithmeticCalculator=null;
//		arithmeticCalculator=new ArithmeticCalculatorLoggingImpl();
		ArithmeticCalculator target=new ArithmeticCalculatorImpl();
		ArithmeticCalculator proxy=new ArithmeticCalculatorLoggingProxy(target).getLoggingProxy();
		int result=proxy.add(1, 2);
		System.out.println("-->"+result);
		result=proxy.div(4, 2);
		System.out.println("-->"+result);
		
	}

}

运行结果:

The method...addbegins with[1, 2]
The methodaddends with 3
-->3
The method...divbegins with[4, 2]
The methoddivends with 2
-->2

Spring AOP可以解决此类问题!

AOP简介

AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面向对象编程) 的补充.

AOP 的主要编程对象是切面(aspect), 切面模块化横切关注点.

在应用 AOP 编程时, 仍然需要定义公共功能, 但可以明确的定义这个功能在哪里, 以什么方式应用, 并且不必修改受影响的类. 这样一来横切关注点就被模块化到特殊的对象(切面).

AOP 的好处:

每个事物逻辑位于一个位置, 代码不分散, 便于维护和升级

业务模块更简洁, 只包含核心业务代码.

 AOP术语

切面(Aspect):  横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象

通知(Advice):  切面必须要完成的工作

目标(Target): 被通知的对象

代理(Proxy): 向目标对象应用通知之后创建的对象

连接点(Joinpoint):程序执行的某个特定位置:如类某个方法调用前、调用后、方法抛出异常后等连接点由两个信息确定:方法表示的程序执行点;相对点表示的方位。例如 ArithmethicCalculator#add() 方法执行前的连接点,执行点为 ArithmethicCalculator#add(); 方位为该方法执行前的位置

切点(pointcut):每个类都拥有多个连接点:例如 ArithmethicCalculator 的所有方法实际上都是连接点,即连接点是程序类中客观存在的事务AOP 通过切点定位到特定的连接点。类比:连接点相当于数据库中的记录,切点相当于查询条件。切点和连接点不是一对一的关系,一个切点匹配多个连接点,切点通过 org.springframework.aop.Pointcut 接口进行描述,它使用类和方法作为连接点的查询条件

Spring AOP

AspectJJava 社区里最完整最流行的 AOP 框架.

Spring2.0 以上版本, 可以使用基于 AspectJ 注解或基于 XML 配置的 AOP

Spring 中启用 AspectJ 注解支持

要在 Spring 应用中使用 AspectJ 注解, 必须在 classpath 下包含 AspectJ 类库: aopalliance.jaraspectj.weaver.jar spring-aspects.jar

aop Schema 添加到 <beans> 根元素中.

Spring IOC 容器中启用 AspectJ 注解支持, 只要Bean 配置文件中定义一个空的 XML 元素 <aop:aspectj-autoproxy>

Spring IOC 容器侦测到 Bean 配置文件中的 <aop:aspectj-autoproxy> 元素时, 会自动为与 AspectJ 切面匹配的 Bean 创建代理.

AspectJ 注解声明切面

要在 Spring 中声明 AspectJ 切面, 只需要在 IOC 容器中将切面声明为 Bean 实例. 当在 Spring IOC 容器中初始化 AspectJ 切面之后, Spring IOC 容器就会为那些与 AspectJ 切面相匹配的 Bean 创建代理.

AspectJ 注解中, 切面只是一个带有 @Aspect 注解的 Java .

通知是标注有某种注解的简单的 Java 方法.

AspectJ 支持 5 种类型的通知注解:

@Before: 前置通知, 在方法执行之前执行

@After: 后置通知, 在方法执行之后执行

@AfterRunning: 返回通知, 在方法返回结果之后执行

@AfterThrowing: 异常通知, 在方法抛出异常之后

@Around: 环绕通知, 围绕着方法执行

举个栗子

前置、返回、后置、异常、环绕通知

前置通知

前置通知:在方法执行之前执行的通知

前置通知使用 @Before 注解, 并将切入点表达式的值作为注解值.

后置通知同理

利用方法签名编写 AspectJ 切入点表达式

最典型的切入点表达式时根据方法的签名来匹配各种方法:

execution * com.atguigu.spring.ArithmeticCalculator.*(..): 匹配 ArithmeticCalculator 中声明的所有方法,第一个 * 代表任意修饰符及任意返回值. 第二个 * 代表任意方法. .. 匹配任意数量的参数. 若目标类与接口与该切面在同一个包中, 可以省略包名.

execution public * ArithmeticCalculator.*(..): 匹配 ArithmeticCalculator 接口的所有公有方法.

execution public double ArithmeticCalculator.*(..): 匹配 ArithmeticCalculator 返回 double 类型数值的方法

execution public double ArithmeticCalculator.*(double, ..): 匹配第一个参数为 double 类型的方法, .. 匹配任意数量任意类型的参数

execution public double ArithmeticCalculator.*(double, double): 匹配参数类型为 double, double 类型的方法.

合并切入点表达式

AspectJ , 切入点表达式可以通过操作符 &&, ||, ! 结合起来.

让通知访问当前连接点的细节

步骤

1).新建bin文件夹,导入 jar 包:

com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar

commons-logging-1.1.1.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar


2).新建配置文件如:applicationContext.xml,在配置文件中加入aop的命名空间

3)
①.基于注解的方式
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.yorkmass.spring.aop.impl"></context:component-scan>
②.在配置文件中加入如下配置
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
③.吧横切关注点的代码抽象到切面的类中
i.切面首先是一个IOC中的bean,即加入@Component注释
ii.切面还需要加入@Aspect注解
④.在类中声明各种通知:
i:声明一个方法
ii:在方法前加入@Before注解
execution(public int com.yorkmass.spring.aop.impl.ArithmeticCalculator.*(int, int))
execution:执行的意思
也可以改为* com.yorkmass.spring.aop.impl.*.*(int, int)
上面表示为任意修饰符任意返回值impl包里面的所有类所有方法
⑤.可以在通知方法中声明一个类型为 JoinPoint 的参数. 然后就能访问链接细节. 如方法名称和参数值. 

@Aspect
@Component
public class LoggingAspect {
    //声明该方法是一个前置通知:在目标方法开始之前执行
    @Before("execution(public int com.yorkmass.spring.aop.impl.ArithmeticCalculator.*(int, int))")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();
        List<Object> args=Arrays.asList(joinPoint.getArgs());
        System.out.println("The method "+methodName+" begins with"+args);
    }  
}

新建一个包com.yorkmass.spring.aop.impl

下面新建3个类:ArithmeticCalculator、ArithmeticCalculatorImpl、LoggingAspect

新建一个测试类Main

程序结构:

ArithmeticCalculator接口

package com.yorkmass.spring.aop.impl;

public interface ArithmeticCalculator {
	int add(int i,int j);
	int sub(int i,int j);
	int mul(int i,int j);
	int div(int i,int j);
	
}

 ArithmeticCalculatorImpl类

package com.yorkmass.spring.aop.impl;

import org.springframework.stereotype.Component;

@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

	@Override
	public int add(int i, int j) {
		int result=i+j;
		return result;
	}

	@Override
	public int sub(int i, int j) {
		int result=i-j;
		return result;
	}

	@Override
	public int mul(int i, int j) {
		int result=i*j;
		return result;
	}

	@Override
	public int div(int i, int j) {
		int result=i/j;
		return result;
	}

}

LoggingAspect类

package com.yorkmass.spring.aop.impl;


import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

//把这个类声明为一个切面:需要把该类放入到IOC容器中,再声明为一个切面
@Aspect
@Component
public class LoggingAspect {
	//声明该方法是一个前置通知:在目标方法开始之前执行
	@Before("execution(public int com.yorkmass.spring.aop.impl.ArithmeticCalculator.*(int, int))")
	public void beforeMethod(JoinPoint joinPoint){
		String methodName=joinPoint.getSignature().getName();
		List<Object> args=Arrays.asList(joinPoint.getArgs());
		System.out.println("The method "+methodName+" begins with"+args);
	}
//后置通知:在目标方法执行后(无论是否发生异常),执行的通知
	//在后置通知中还不能访问目标方法执行的结果
	@After("execution(* com.yorkmass.spring.aop.impl.*.*(int,int))")
	public void afterMethod(JoinPoint joinPoint){
		String methodName=joinPoint.getSignature().getName();
		System.out.println("The method "+methodName+"ends");
	}
	/**
	 * 在方法正常结束后执行的代码
	 * 返回通知是可以访问到方法的返回值的!
	 * @param joinPoint
	 */
	@AfterReturning(value="execution(public int com.yorkmass.spring.aop.ArithmeticCalculator.*(..))",
			returning="result")
	public void afterReturning(JoinPoint joinPoint,Object result){
		String methodName=joinPoint.getSignature().getName();
		System.out.println("The method "+methodName+" ends with "+result);
	}
	
	/**
	 * 在方法出现异常时会执行的代码
	 * 可以访问到异常对象,且可以指定在出现特定异常时再执行通知代码
	 * @param joinPoint
	 * @param ex
	 */
	@AfterThrowing(value="execution(public int com.yorkmass.spring.aop.ArithmeticCalculator.*(..))",
			throwing="ex")
//	public void afterThrowing(JoinPoint joinPoint,Exception ex){
//		String methodName=joinPoint.getSignature().getName();
//		System.out.println("The method "+methodName+" Throwing:"+ex);
//	}
	public void afterThrowing(JoinPoint joinPoint,NullPointerException ex){
		String methodName=joinPoint.getSignature().getName();
		System.out.println("The method "+methodName+" Throwing:"+ex);
	}
	
	/**
	 * 环绕通知需要携带ProceedingJoinPoint类型的参数
	 * 环绕通知相当于动态代理的全过程,ProceedingJoinPoint类型的参数可以决定是否执行目标方法。
	 * 且环绕通知必须有返回值,返回值即为目标方法的返回值
	 */
	@Around("execution(public int com.yorkmass.spring.aop.ArithmeticCalculator.*(..))")
	public Object aroundMethod(ProceedingJoinPoint pjd){
	
		Object result=null;
		String methodName=pjd.getSignature().getName();
		
		//执行目标方法
	try {
		//前置通知
		System.out.println("The method "+methodName+" begin with: "+Arrays.asList(pjd.getArgs()));
		result=pjd.proceed();
		//返回通知
		System.out.println("The method "+methodName+" end with:"+result);
		
	} catch (Throwable e) {
		// TODO: handle exception
		//异常通知
		System.out.println("The method "+methodName+" occurs exception:"+e);
		throw new RuntimeException(e);
	}
	//后置通知
	System.out.println("The method "+methodName+" end with");
	return result;
	}
	
}

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

	<!-- 配置自动扫描的包 -->
	<context:component-scan base-package="com.yorkmass.spring.aop.impl"></context:component-scan>
	<!-- 使AspjectJ 注解起作用 :自动为匹配的类生成代理对象-->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

Main类

package com.yorkmass.spring.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		ArithmeticCalculator arithmeticCalculator=(ArithmeticCalculator)ctx.getBean("arithmeticCalculator");
		int result=arithmeticCalculator.add(1, 5);
		System.out.println("result:"+result);
		result=arithmeticCalculator.div(1000, 0);
		System.out.println("result:"+result);
	}
	


}

运行结果:

一月 19, 2019 1:39:50 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c2faae: startup date [Sat Jan 19 13:39:50 CST 2019]; root of context hierarchy
一月 19, 2019 1:39:50 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
The method add begin with: [1, 5]
The method add begins with [1, 5]
The method add end with:6
The method add end with
The method add ends with 
The method add ends with 6
result:6
The method div begin with: [1000, 0]
The method div begins with [1000, 0]
The method div occurs exception:java.lang.ArithmeticException: / by zero
The method div ends with 
Exception in thread "main" java.lang.RuntimeException: java.lang.ArithmeticException: / by zero
	at com.yorkmass.spring.aop.LoggingAspect.aroundMethod(LoggingAspect.java:86)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
	at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:43)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:58)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
	at com.sun.proxy.$Proxy11.div(Unknown Source)
	at com.yorkmass.spring.aop.Main.main(Main.java:15)
Caused by: java.lang.ArithmeticException: / by zero
	at com.yorkmass.spring.aop.ArithmeticCalculatorImpl.div(ArithmeticCalculatorImpl.java:31)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
	at com.yorkmass.spring.aop.LoggingAspect.aroundMethod(LoggingAspect.java:78)
	... 19 more

 

posted @ 2019-01-18 20:07  yorkmass  阅读(207)  评论(0编辑  收藏  举报