<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nadoutong</groupId>
<artifactId>spring_day04_adviceType</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="accountService" class="com.nadoutong.service.impl.AccountServiceImpl" ></bean>
<bean id="logger" class="com.nadoutong.util.Logger"></bean>
<aop:config>
<aop:pointcut id="pt1" expression="execution(* com.nadoutong..service.impl.*.*(..))"/>
<aop:aspect id="logAdvice" ref="logger">
<aop:around method="aroudadvice" pointcut-ref="pt1"></aop:around>
</aop:aspect>
</aop:config>
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
package com.nadoutong.service;
public interface AccountService {
void transfer(int a, double b);
}
package com.nadoutong.service.impl;
import com.nadoutong.service.AccountService;
public class AccountServiceImpl implements AccountService {
public void transfer(int a,double b) {
System.out.println("转账成功");
}
}
package com.nadoutong.util;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.CodeSignature;
import java.util.HashMap;
import java.util.Map;
public class Logger {
public void beforeadvice(){
System.out.println("前置通知");
}
public void afteradvice(){
System.out.println("后置通知");
}
public void throwingadvice(){
System.out.println("异常通知");
}
public void finnaladvice(){
System.out.println("最终通知");
}
public Object aroudadvice(ProceedingJoinPoint pjp){
try {
System.out.println("前置通知");
Object[] args = pjp.getArgs();
String[] paramNames = ((CodeSignature) pjp.getSignature()).getParameterNames();
for (int i = 0; i < args.length; i++) {
System.out.println("参数名称"+paramNames[i]+";参数值:"+args[i]);
}
Object proceed = pjp.proceed(args);
System.out.println("后置通知");
return proceed;
} catch (Throwable throwable) {
System.out.println("异常通知");
throw new RuntimeException();
} finally {
System.out.println("最终通知");
}
}
}
package com.nadoutong;
import com.nadoutong.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AOPTest {
@org.junit.Test
public void test01(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
AccountService accountService=(AccountService)applicationContext.getBean("accountService");
accountService.transfer(1,2);
}
}
- 测试结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210515165737686.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzU2OTgxMTg1,size_16,color_FFFFFF,t_70)