SpringAOP原理

-----------------------------------Spring AOP----------------------------------------

Spring AOP ,即面向切面编程。

Spring中AOP代理由Spring的IOC容器负责生成、管理,其依赖关系也由IOC容器负责管理。因此,AOP代理可以直接使用容器中的其它bean实例作为目标,这种关系可由IOC容器的依赖注入提供。

   前提条件:将需要aop的类加入ioc容器,将切面类加入ioc容器

     1). 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/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  
  <!-- 将扫描com.spring.busi包以及子包下的 带有@Controller,@Component,@Service,@Repository 的bean,纳入ioc容器 -->
  <context:component-scan base-package="com.spring.framework"></context:component-scan>
	<!-- 开始spring动态代理 -->
  <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  
  <!-- spring aop xml配置方法 -->
  <bean id="loggerAspect" class="com.spring.framework.Aop.LoggerAspect"></bean>
  <aop:config>
       <!-- 配置切点表达式 -->
      <aop:pointcut expression="execution(public * com.spring.framework.Aop.CalculatorImpl.*(..))" id="pointcut"/>
      <aop:aspect ref="loggerAspect">
          <aop:before method="beforeMethod" pointcut="pointcut"/>
      </aop:aspect>
  </aop:config>
</beans>

    2).注解方式实现

package com.spring.framework.Aop;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


//将该类声明为一个切面: 需要把类放入ioc容器@Component ,再声明一个切面@Aspect
@Component
@Aspect
public class LoggerAspect {

	@Pointcut("execution(public * com.spring.framework.Aop.CalculatorImpl.*(..))")
	public void pointcut(){
		
	}
	//声明该方法为前置通知:在目标方法之前执行
	@Before("pointcut()")
	public void beforeMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("methodName :"+methodName);
		System.out.println("beforeMethod ..");
	}
	
	//后置通知在目标方法执行后执行(无论是否发生异常,发生异常也会执行)
	@After("pointcut()")
	public void afterMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("methodName :"+methodName);
		System.out.println("afterMethod ..");
	}
	@AfterReturning(returning="result" , pointcut="pointcut()")
	public void afterReturning(JoinPoint joinPoint, Object result){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("methodName :"+methodName +", result:"+result);
	}
	
	/**
	 * 环绕通知需要携带参数ProceedingJoinPoint
	 * @param pjp
	 * @return
	 */
	@Around("pointcut()")
	public Object AroundMethod(ProceedingJoinPoint pjd){
		Object result = null;
		String methodName = pjd.getSignature().getName();
		try {
			//前置通知
			System.out.println("Before--methodName :"+methodName +", param :"+ Arrays.asList(pjd.getArgs()));
			//执行目标方法
			result=pjd.proceed();
			//后置通知
			System.out.println("After--methodName :"+methodName +", result :"+ result);
		} catch (Throwable e) {
			e.printStackTrace();
			//异常通知
			System.out.println("Exception--methodName :"+methodName +", exception :"+ e);
		}
		//后置通知
		System.out.println("After--methodName :"+methodName +", ends ....");
		return result;
	}
}


Spring AOPAspect-Oriented Programming)是一种基于面向切面编程的技术,它通过将一个应用程序分解成许多独立的部分,从而提高了应用程序的模块化程度,同时也提高了代码的可重用性和可维护性。 Spring AOP 是在运行时对程序进行修改的,它不需要重新编译源代码,因此使得代码的维护和修改更加方便。Spring AOP 原理主要分为以下几个方面: 1. 切面(Aspect):切面是一个类,其中包含了一组相关的Advice和其他相关的代码,用于在目标对象的方法执行前、执行后或者抛出异常时执行相应的操作。 2. 连接点(Join point):连接点是指在应用程序的执行过程中,可以被切面拦截的点,比如方法调用、异常抛出、属性赋值等。 3. 通知(Advice):通知是指在连接点处执行的代码,有多种类型的通知,包括前置通知(Before advice)、后置通知(After advice)、返回通知(After returning advice)、异常通知(After throwing advice)和环绕通知(Around advice)。 4. 切入点(Pointcut):切入点是一个表达式,用于定义哪些连接点会被切面拦截。 5. 代理(Proxy):代理是指在目标对象和切面之间创建的对象,它可以拦截目标对象的方法调用,并在方法执行前后执行相应的通知。 Spring AOP 的实现机制主要是通过 JDK 动态代理或者 CGLIB 动态代理来实现的。当目标对象实现了接口时,Spring AOP 使用 JDK 动态代理,否则使用 CGLIB 动态代理。在 JDK 动态代理中,代理对象实现了与目标对象相同的接口,而在 CGLIB 动态代理中,代理对象是目标对象的子类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值