Spring学习笔记(三)——AOP代理对象

1、日志工具类:LogUtils.Java

package com.atguigu.utils;

import java.lang.reflect.Method;
import java.util.Arrays;

import javax.management.RuntimeErrorException;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
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.stereotype.Component;

@Aspect
@Component
public class LogUtils {
	
	//目标方法执行之前
	@Before("MyPoint()")
	public static void logStart(JoinPoint joinPoint) {
		Object[] args = joinPoint.getArgs();
		Signature signature = joinPoint.getSignature();
		System.out.println("Before【"+signature.getName()+"】方法开始执行,用的参数列表" + Arrays.asList(args));
	}
	 
	//目标方法正常执行完成之后执行
	@AfterReturning(value="MyPoint()",returning="result")
	public static void logReturn(JoinPoint joinPoint,Object result) {
		Signature signature = joinPoint.getSignature();
		System.out.println("Return【"+signature.getName()+"】方法正常执行完成,return:"+result);
	}
	
	//目标方法出现异常执行
	@AfterThrowing(value="MyPoint()",throwing="exception")
	public static void logException(JoinPoint joinPoint,Exception exception) {
		Signature signature = joinPoint.getSignature();
		System.out.println("Exception【"+signature.getName()+"】方法执行出现异常,Exception:"+exception);
	}
	
	//目标方法最终结束的时候执行
	@After("MyPoint()")
	public static void logEnd(JoinPoint joinPoint) {
		Signature signature = joinPoint.getSignature();
		System.out.println("End【"+signature.getName()+"】方法执行结束,用的参数列表4");
	}
	
	@Pointcut("execution(public int com.atguigu.impl.MyMathCalculator.*(int, int))")
	public void MyPoint(){}
	
	@Around("MyPoint()")
	public Object myAround(ProceedingJoinPoint pjp) throws Throwable{
		Object[] args = pjp.getArgs();
		Object proceed = null;
		String name = pjp.getSignature().getName();
		try {
			//@Before
			System.out.println("【环绕前置通知】【"+name+"方法开始】");
			//利用反射调用目标方法,也就是method.invoke(obj,args)
			proceed = pjp.proceed();
			//@AfterReturning
			System.out.println("【环绕返回通知】【"+name+"方法返回,返回值"+proceed+"】");	
		} catch (Exception e){
			//@AfterThrowing
			System.out.println("【环绕异常通知】【"+name+"方法出现异常,异常信息:"+e+"】");
			throw new RuntimeException(e);
		} finally {
			//@After
			System.out.println("【环绕后置通知】【"+name+"方法结束】");
		}
		
		return proceed;
	}
}

2、@Service业务类:MyMathCalculator.java

package com.atguigu.impl;

import org.springframework.stereotype.Service;

import com.atguigu.inter.Calculator;

@Service
public class MyMathCalculator /*implements Calculator*/{

//	@Override
	public int add(int i, int j) {
		return i + j;
	}

//	@Override
	public int sub(int i, int j) {
		return i - j;
	}

//	@Override
	public int mul(int i, int j) {
		return i * j;
	}

//	@Override
	public int div(int i, int j) {
		return i / j;
	}
	
}

3、xml配置: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:context="http://www.springframework.org/schema/context"
	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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

	<context:component-scan base-package="com.atguigu"></context:component-scan>
	
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	
</beans>

4.测试:AOPTest.java

package com.atguigu.test;

import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.atguigu.impl.MyMathCalculator;
import com.atguigu.inter.Calculator;

public class AOPTest {

	ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
	@Test
	public void test() {
//		Calculator proxy = CalculatorProxy.getProxy(calculator);
//		Calculator bean = ioc.getBean(Calculator.class);
//		bean.add(2, 1);
		MyMathCalculator bean = ioc.getBean(MyMathCalculator.class);
		bean.add(1, 2);
	}

}

 

发布了3 篇原创文章 · 获赞 0 · 访问量 30
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览