<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
<!-- 自动扫报 -->
<context:component-scan base-package="com.song"/>
<!-- 代理对象 -->
<aop:aspectj-autoproxy/>
</beans>
package com.song.dao;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
@Aspect
public class LoggerAspect {
@Before("execution(public int com.song.dao.CalImpl.*(..))")
public void before(JoinPoint joinPoint){
//获取方法名字
String name = joinPoint.getSignature().getName();
//获取参数
String args = Arrays.toString(joinPoint.getArgs());
System.out.println(name+"参数是"+args);
}
@After(value = "execution(public int com.song.dao.CalImpl.* (..))")
public void after(JoinPoint joinPoint){
//获取⽅法名
String name = joinPoint.getSignature().getName();
System.out.println(name+"⽅法执⾏完毕");
}
@AfterReturning(value = "execution(public int com.song.dao.CalImpl.* (..))",returning = "result")
public void after1(JoinPoint joinPoint, Object result){
String name = joinPoint.getSignature().getName();
System.out.println(name+"的结果是"+result);
}
@AfterThrowing(value = "execution(public int com.song.dao.CalImpl.*(..))",throwing = "exception")
public void afterThrowing(JoinPoint joinPoint,Exception exception){
//获取⽅法名
String name = joinPoint.getSignature().getName();
System.out.println(name+"⽅法抛出异常:"+exception);
}
}
package com.song.dao;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
public class CalImpl implements Cal{
public int add(int num1, int num2) {
int result = num1+num2;
return result;
}
public int sub(int num1, int num2) {
int result = num1-num2;
return result;
}
public int mul(int num1, int num2) {
int result = num1*num2;
return result;
}
public int div(int num1, int num2) {
int result = num1/num2;
return result;
}
}
package com.song.dao;
public interface Cal {
public int add(int num1,int num2);
public int sub(int num1,int num2);
public int mul(int num1,int num2);
public int div(int num1,int num2);
}
import com.song.dao.Cal;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testAop {
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("aop.xml");
Cal calImpl =(Cal) applicationContext.getBean("calImpl");
calImpl.add(1,1);
calImpl.sub(134,2);
calImpl.div(10,2);
calImpl.mul(1,3);
}
}