Spring AOP的几种实现方式

本文介绍了AOP(面向切面编程)的核心概念,包括横切关注点、切面、连接点、切入点、通知等,并详细阐述了Spring框架下实现AOP的多种方式,如Java动态代理、CGLIB生成代理、BeanNameAutoProxyCreator及AspectJ注解等。

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

<think>好的,我现在需要回答用户关于Spring AOP实现方式的问题。首先,我得回忆一下Spring AOP的基本概念和实现方式。根据用户提供的引用内容,Spring AOP主要是通过动态代理来实现的,具体有两种方式:基于JDK的动态代理和基于CGLIB的动态代理。此外,可能还涉及到AspectJ的集成,但需要区分Spring AOP和AspectJ的不同。 接下来,我需要确认用户的问题核心是“Spring AOP实现方式”,特别是切面编程的具体方法。用户可能想知道不同的实现手段以及它们的区别。根据引用[1]和[2],Spring AOP主要使用动态代理,而引用[2]提到Spring Boot中的AOP实现可能与AspectJ结合使用,但需要明确Spring AOP本身是否支持其他方式。 首先,要明确两种代理方式的适用场景:JDK动态代理适用于接口实现的类,而CGLIB可以代理没有接口的类。这点很重要,需要详细说明。然后,可能还需要提到如何通过配置选择使用哪种代理方式,比如在Spring配置中设置proxy-target-class属性。 另外,用户可能对AspectJ的注解支持感兴趣,比如@Aspect、@Pointcut、@Around等,这些是Spring AOP中定义切面的常用方式。需要解释如何通过注解声明切面,并将其纳入Spring管理。 还需要考虑XML配置的方式,虽然现在可能更常用注解,但传统项目可能仍在使用XML配置AOP,这也是实现方式之一。因此,应该涵盖注解和XML两种配置方法。 然后,需要比较Spring AOP和AspectJ的区别,引用[2]提到了两者的对比,可能需要说明Spring AOP是基于代理的,而AspectJ是在编译时或加载时织入,更强大但更复杂。这部分有助于用户理解不同实现方式的优缺点。 最后,根据用户提供的示例,回答需要结构清晰,分点列出,可能包括实现方式、配置方法、注解使用、代理类型比较等。同时,确保所有LaTeX格式正确,如类名或方法名使用行内格式,独立公式如果有的话使用$$,但可能这里用不上太多数学公式,更多的是代码示例。 检查是否有遗漏的部分,比如是否需要提到AOP的应用场景或优势,但用户问题集中在实现方式,所以应集中在技术细节上。确保引用的正确性,引用[1]、[2]、[3]中的相关内容需要适当标注。 总结步骤: 1. 介绍Spring AOP的两种动态代理实现方式:JDK和CGLIB,解释它们的区别和适用场景。 2. 说明基于注解的配置方式,如@Aspect、@Pointcut等,以及如何启用注解支持。 3. 提及XML配置方式,作为传统方法。 4. 比较Spring AOP与AspectJ的不同,帮助用户理解选择依据。 5. 提供代码示例,展示如何定义切面和使用注解。 6. 确保回答符合格式要求,正确使用引用标注,并生成相关问题。 现在需要组织这些点,确保逻辑连贯,语言准确,符合中文表达习惯,并正确应用引用标注。</think>### Spring AOP实现方式 Spring AOP主要通过**动态代理技术**实现面向切面编程,其核心实现方式可分为以下两类: --- #### 1. **基于JDK动态代理** - **适用场景**:当目标类实现了接口时,Spring AOP默认使用JDK动态代理。代理对象会实现相同的接口,并在调用目标方法前后插入切面逻辑。 - **特点**: - 仅支持接口代理,无法代理未实现接口的类。 - 运行时生成代理类,性能开销较小。 - **示例**: ```java public interface UserService { void saveUser(); } public class UserServiceImpl implements UserService { public void saveUser() { System.out.println("保存用户"); } } ``` JDK动态代理会为`UserService`接口生成代理对象,拦截方法调用[^1]。 --- #### 2. **基于CGLIB动态代理** - **适用场景**:当目标类未实现接口时,Spring AOP会使用CGLIB库生成目标类的子类作为代理。 - **特点**: - 可以代理普通类(无需接口)。 - 通过继承实现代理,因此`final`类或方法无法被代理。 - 性能略低于JDK动态代理,但功能更灵活。 - **配置方式**: 在Spring配置中设置`proxy-target-class=true`,强制使用CGLIB代理: ```xml <aop:aspectj-autoproxy proxy-target-class="true"/> ``` 或通过注解启用: ```java @EnableAspectJAutoProxy(proxyTargetClass = true) ``` --- #### 3. **基于注解的切面定义** Spring AOP支持通过`@Aspect`注解声明切面,结合以下注解定义通知(Advice): - **`@Pointcut`**:定义切入点表达式,指定哪些方法需要被拦截。 - **`@Before`/`@After`/`@Around`**:定义前置通知、后置通知和环绕通知。 - **示例**: ```java @Aspect @Component public class LoggingAspect { @Pointcut("execution(* com.example.service.*.*(..))") public void serviceMethods() {} @Around("serviceMethods()") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long start = System.currentTimeMillis(); Object result = joinPoint.proceed(); long end = System.currentTimeMillis(); System.out.println("方法执行时间:" + (end - start) + "ms"); return result; } } ``` 需在配置类中添加`@EnableAspectJAutoProxy`以启用注解支持[^2][^3]。 --- #### 4. **基于XML的切面配置(传统方式)** 通过XML文件定义切面和通知,适用于早期Spring版本或需要解耦代码的场景: ```xml <aop:config> <aop:aspect ref="loggingAspect"> <aop:pointcut id="serviceMethods" expression="execution(* com.example.service.*.*(..))"/> <aop:around pointcut-ref="serviceMethods" method="logExecutionTime"/> </aop:aspect> </aop:config> ``` 需在Bean定义中注入切面类`loggingAspect`。 --- #### 对比:Spring AOP与AspectJ | 特性 | Spring AOP | AspectJ | |---------------------|--------------------------------|----------------------------------| | **实现方式** | 动态代理(运行时织入) | 编译时/加载时织入 | | **性能** | 适用于轻量级场景 | 更高性能,适合复杂切面 | | **功能范围** | 仅支持方法级别拦截 | 支持字段、构造方法等更细粒度控制 | | **依赖** | 无需额外编译步骤 | 需编译器或类加载器支持 | ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值