横切关注点:跨越应用程序多个模块的方法或功能。(软件系统,可以看做由一组关注点即业务或功能或方法组成。其中,直接的业务关注点是直切关注点,而为直切关注点服务的,就是横切关注点。)即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。
切面(ASPECT):横切关注点被模块化的特殊对象。即,它是一个类。
通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
目标(Target):被通知对象。
代理(Proxy):向目标对象应用通知之后创建的对象。
切入点(PointCut):切面通知执行的“地点”的定义。
连接点(JointPoint):与切入点匹配的执行点。
下面示意图:
SpringAOP中,通过Advice定义横切逻辑,Spring中支持5种类型的Advice:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.0.RELEASE</version> </dependency>
前置通知:org.springframework.aop.MethodBeforeAdvice;
package com.chenjiahao.spring.springdynamicproxy; import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; /** * 前置通知 * */ public class BeforeAdvice implements MethodBeforeAdvice { /** * method 方法信息 * args 参数 * target 被代理的目标对象 */ public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("----前置通知----"); } }
后置通知:org.springframework.aop.AfterReturningAdvice;
package com.chenjiahao.spring.springdynamicproxy; import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method; /** * 后置通知 * */ public class AfterAdvice implements AfterReturningAdvice { /* * returnValue 返回值 * method 被调用的方法 * args 方法参数 * target 被代理对象 */ public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("----后置通知----"); } }
环绕通知:org.aopalliance.intercept.MethodInterceptor;
package com.chenjiahao.spring.springdynamicproxy; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; /** * 环绕通知 * 方法拦截器 * */ public class SurroundAdvice implements MethodInterceptor { public Object invoke(MethodInvocation mi) throws Throwable { /** *前置横切逻辑 * getMethod() 获得方法 * getThis() 获得被调用的的对象 * getArguments() 获得参数 * */ System.out.println("----环绕方法前置----:方法" + mi.getMethod() + " 被调用在对象" + mi.getThis() + "上,参数 " + mi.getArguments()); //方法调用 Object result = mi.proceed(); //后置横切逻辑 System.out.println("----环绕方法后置----:返回值:"+ result); return result; } }
package com.chenjiahao.spring.springdynamicproxy; import org.springframework.aop.framework.ProxyFactory; /** * 动态代理类 * */ public class SpringDynamic { /** * target 传入的实际对象 * */ public static<T> T getProxy(T target){ //创建Spring代理工厂 ProxyFactory proxyFactory=new ProxyFactory(); //设置代理对象 proxyFactory.setTarget(target); //添加通知方法 可以添加多个 proxyFactory.addAdvice(new SurroundAdvice()); //返回代理对象 Object obj=proxyFactory.getProxy(); return (T)obj; } }
示例:
package com.chenjiahao.spring.springdynamicproxy; public class Client { public static void main(String[] args) { IMant mant = SpringDynamic.getProxy(new Mant()); mant.add(2, 4); } }
结果:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.0.RELEASE</version> </dependency>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--目标对象--> <bean id="target" class="com.chenjiahao.spring.iocdynamicproxy.Mant"></bean> <!--通知 如:前置通知 后置通知 环绕通知等...--> <bean id="advice" class="com.chenjiahao.spring.iocdynamicproxy.SurroundAdvice"></bean> <!--代理对象 --> <!--interceptorNames 通知数组 --> <!--p:target-ref 被代理的对象--> <!--p:proxyTargetClass 被代理对象是否为一个类,如果是则使用cglib,否则使用jdk动态代理 --> <bean id="dynamicproxy" class="org.springframework.aop.framework.ProxyFactoryBean" p:interceptorNames="advice" p:target-ref="target" p:proxyTargetClass="true"> </bean> </beans>
package com.chenjiahao.spring.iocdynamicproxy; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; /** * 环绕通知 * 方法拦截器 * */ public class SurroundAdvice implements MethodInterceptor { public Object invoke(MethodInvocation mi) throws Throwable { /** *前置横切逻辑 * getMethod() 获得方法 * getThis() 获得被调用的的对象 * getArguments() 获得参数 * */ System.out.println("----环绕方法前置----:方法" + mi.getMethod() + " 被调用在对象" + mi.getThis() + "上,参数 " + mi.getArguments()); //方法调用 Object result = mi.proceed(); //后置横切逻辑 System.out.println("----环绕方法后置----:返回值:"+ result); return result; } }
示例:
package com.chenjiahao.spring.iocdynamicproxy; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) { //获得容器 ApplicationContext ctx=new ClassPathXmlApplicationContext("springcfgproxy.xml"); IMant mant= (IMant) ctx.getBean("dynamicproxy"); mant.add(2, 4); mant.sub(2, 4); mant.mul(2, 4); mant.div(2, 4); } }
结果:
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency>
package com.chenjiahao.spring.springxmldynamicprocy; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class Advices { //前置通知 public void beforeMethod(JoinPoint joinPoint) { System.out.println("----前置通知----"); } //后置通知 public void afterMethod(JoinPoint joinPoint) { System.out.println("----后置通知----"); } //返回值通知 public void afterReturning(JoinPoint joinPoint, Object result) { System.out.println("----返回值通知----"+result); } //抛出异常通知 //在方法出现异常时会执行的代码可以访问到异常对象,可以指定在出现特定异常时在执行通知代码 public void afterThrowing(JoinPoint joinPoint, Exception ex) { System.out.println("----抛出异常通知----"+ex.getMessage()); } //环绕通知 //环绕通知需要携带ProceedingJoinPoint类型的参数 //环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行目标方法。 //而且环绕通知必须有返回值,返回值即为目标方法的返回值 public Object aroundMethod(ProceedingJoinPoint pjd) { Object object = null; try { System.out.println("----环绕通知前置----"); object = pjd.proceed(); System.out.println("----环绕通知后置----"); } catch (Throwable throwable) { throwable.printStackTrace(); } return object; } }
<?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: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-4.3.xsd"> <!--被代理对象--> <bean id="math" class="com.chenjiahao.spring.springxmldynamicprocy.Mant"></bean> <!--定义的通知--> <bean id="advice" class="com.chenjiahao.spring.springxmldynamicprocy.Advices"></bean> <!--AOP配置--> <!-- proxy-target-属性class表示被代理的类是否为一个没有实现接口的类,Spring会依据实现了接口则使用JDK内置的动态代理,如果未实现接口则使用cblib --> <aop:config proxy-target-class="true"> <!-- 切面配置 --> <!--ref表示通知对象的引用 --> <aop:aspect ref="advice"> <!-- 配置切入点(横切逻辑将注入的精确位置) --> <!--execution 第一个参数为访问修饰符如 public...等--> <!--com.chenjiahao.spring.springxmldynamicprocy.Mant.*(..)) 包下面的Mant类中的所有方法任易参数--> <aop:pointcut expression="execution(* com.chenjiahao.spring.springxmldynamicprocy.Mant.*(..))" id="pointcut1"/> <!--声明通知,method指定通知类型,pointcut指定切点,就是该通知应该注入那些方法中 --> <aop:before method="beforeMethod" pointcut-ref="pointcut1"/> <aop:after method="afterMethod" pointcut-ref="pointcut1"/> <aop:around method="aroundMethod" pointcut="execution(* com.chenjiahao.spring.springxmldynamicprocy.Mant.*(..))"></aop:around> <!--throwing 方法执行后的返回结果--> <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut1" throwing="ex"></aop:after-throwing> </aop:aspect> </aop:config> </beans>
参数解读:
aop:config:AOP配置
proxy-target-class:表示被代理的类是否为一个没有实现接口的类,Spring会依据实现了接口则使用JDK内置的动态代理,如果未实现接口则使用cblib
aop:aspect:切面配置
ref:表示通知对象的应用(连接定义好的通知类)
aop:pointcut:切点配置 表达式只需要选其中一个
(1) execution
execution是使用的最多的一种Pointcut表达式,表示某个方法的执行,其标准语法如下。
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
modifiers-pattern表示方法的访问类型,public等;
ret-type-pattern表示方法的返回值类型,如String表示返回类型是String,“*”表示所有的返回类型;
declaring-type-pattern表示方法的声明类,如“com.elim..*”表示com.elim包及其子包下面的所有类型;
name-pattern表示方法的名称,如“add*”表示所有以add开头的方法名;
param-pattern表示方法参数的类型,name-pattern(param-pattern)其实是一起的表示的方法集对应的参数类型,如“add()”表示不带参数的add方法,“add(*)”表示带一个任意类型的参数的add方法,“add(*,String)”则表示带两个参数,且第二个参数是String类型的add方法;
throws-pattern表示异常类型;其中以问号结束的部分都是可以省略的。
(2) within
within是用来指定类型的,指定类型中的所有方法将被拦截。
(3) this
Spring Aop是基于代理的,this就表示代理对象。this类型的Pointcut表达式的语法是this(type),当生成的代理对象可以转换为type指定的类型时则表示匹配。基于JDK接口的代理和基于CGLIB的代理生成的代理对象是不一样的。
(4) target
Spring Aop是基于代理的,target则表示被代理的目标对象。当被代理的目标对象可以被转换为指定的类型时则表示匹配。
(5) args
args用来匹配方法参数的。
(6) @target
@target匹配当被代理的目标对象对应的类型及其父类型上拥有指定的注解时。
(7) @args
@args匹配被调用的方法上含有参数,且对应的参数类型上拥有指定的注解的情况。
(8) @within
@within用于匹配被代理的目标对象对应的类型或其父类型拥有指定的注解的情况,但只有在调用拥有指定注解的类上的方法时才匹配。
(9) @annotation
@annotation用于匹配方法上拥有指定注解的情况。
(10) bean
bean用于匹配当调用的是指定的Spring的某个bean的方法时。
(11)表达式组合
表达式的组合其实就是对应的表达式的逻辑运算,与、或、非。可以通过它们把多个表达式组合在一起。
示例:
package com.chenjiahao.spring.springxmldynamicprocy; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) { //获得容器 ApplicationContext ctx=new ClassPathXmlApplicationContext("springxmlcfgdynameproxy.xml"); IMant mant= (IMant) ctx.getBean("math"); mant.add(2, 0); mant.sub(2, 0); mant.mul(2, 0); mant.div(2, 0); } }
结果:
原文:https://www.cnblogs.com/NiuBiHH/p/9809330.html