Spring AOP的几种实现方式

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 Abstration {
    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在拦截方法后,需要返回一个方法执行结果,否则,原方法不能正常执行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值