AOP核心概念
1、横切关注点
对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点
2、切面(aspect)
类是对物体特征的抽象,切面就是对横切关注点的抽象
3、连接点(joinpoint)
被拦截到的点,因为spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
4、切入点(pointcut)
对连接点进行拦截的定义
5、通知(advice)
所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类
6、目标对象
代理的目标对象
7、织入(weave)
将切面应用到目标对象并导致代理对象创建的过程
8、引入(introduction)
在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段
Spring 实现AOP所需要的包:
1、Spring提供的jar包
2、aopalliance.jar
3、aspectjweaver.jar
Spring 实现AOP的方式:
1、Java动态代理
该方法针对接口的实例创建代理
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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean id="concreteImplementor" class="com.marving.aop.ConcreteImplementor" />
<bean id="interceptorHandler" class="com.marving.aop.InterceptorHandler" />
<aop:config>
<aop:aspect id="interceptor" ref="interceptorHandler">
<aop:pointcut id="addAllMethod" expression="execution(* com.marving.aop.Abstration.*(..))" />
<aop:before method="doSomething" pointcut-ref="addAllMethod" />
<aop:after method="doSomething" pointcut-ref="addAllMethod" />
</aop:aspect>
</aop:config>
</beans>
其中Abstration为接口,ConcreteImplementor为实现类,InterceptorHandler为代理拦截类。
public interface <span >Abstration</span> {
public void operation()
}
//具体实现化角色
public class ConcreteImplementor implements Implementor{
@Override
public void operation() {
System.out.println("ConcreteImplementor");
}
}
public class InterceptorHandler{
public void printTime(){
System.out.println("CurrentTime = " + System.currentTimeMillis());
}
}
2、CGLIB生成代理
CGLIB针对代理对象为类的情况使用。
通过实现MethodInterceptor接口,并实现 public Object intercept(Object obj, Method m, Object[] args,MethodProxy proxy) throws Throwable方法生成代理。
3、BeanNameAutoProxyCreator实现AOP
Spring为我们提供了自动代理机制,让容器为我们自动生成代理,把我们从烦琐的配置工作中解放出来,在内部,Spring 使用BeanPostProcessor自动地完成这项工作。
具体配置如下:
<bean id="MyInterceptor" class="com.yesjpt.interceptor. MyInterceptor"></bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>MyInterceptor</value>
</list>
</property>
</bean>
其中*Service 为需要拦截代理的bean,以Service结尾的都 被拦截,并使用MyInterceptor 进行拦截,可配置多个拦截器,按顺序执行。
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* @author
*
*/
public class MyInterceptor implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();//获取被拦截的方法
Object[] arguments = invocation.getArguments();//获取拦截方法的参数
/*
* 特殊,某些权限需要做特殊处理
* 比如用户信息权限,在方法执行完毕返回的时候,要将电话号码与邮箱抹除
*/
//环绕通知前置特殊处理
this.beforeReslove();
Object proceed = invocation.proceed();//调用目标方法
//环绕通知后置特殊处理
proceed = this.afterReslove();
return proceed;
}
private Object afterReslove() {
System.out.println("CurrentTime = " + System.currentTimeMillis());
return null;
}
private void beforeReslove() {
System.out.println("CurrentTime = " + System.currentTimeMillis());
}
}
4、使用注解AspectJ实现AOP
ApplicationContext.xml 加入
<aop:aspectj-autoproxy/>
创建切面处理类
package com.marving.aop;
import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AspectHandler {
@Pointcut("execution(* com.marving.service.BaseServ+.*(..))")
private void doMethod() {
}
/**
* This is the method which I would like to execute before a selected method
* execution.
*/
@Before("doMethod()")
public void beforeAdvice() {
System.out.println("before method invoked.");
}
/**
* This is the method which I would like to execute after a selected method
* execution.
*/
@After("doMethod()")
public void afterAdvice() {
System.out.println("after method invoked.");
}
// 配置controller环绕通知,使用在方法aspect()上注册的切入点
@Around("doMethod()")
public Object around(ProceedingJoinPoint pjp) throws Throwable{
Object result = null;
String methodName = pjp.getSignature().getName();
try {
System.out.println("The method [" + methodName + "] begins with " + Arrays.asList(pjp.getArgs()));
result = pjp.proceed();
} catch (Throwable e) {
System.out.println("The method [" + methodName + "] occurs expection : " + e);
throw new RuntimeException(e);
}
System.out.println("The method [" + methodName + "] ends");
return result;
}
}
通过表达式execution(* com.marving.service.BaseServ+.*(..)) 匹配切入点函数,并使用@Before@After@Around 对所拦截方法执行前、中、后进行拦截并执行处理函数。
@Around @Before @After三个注解的区别@Before是在所拦截方法执行之前执行一段逻辑。@After 是在所拦截方法执行之后执行一段逻辑。@Around是可以同时在所拦截方法的前后执行一段逻辑。
值得注意的是,Around在拦截方法后,需要返回一个方法执行结果,否则,原方法不能正常执行。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。