logger日志类:
package cn.mepu.utils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; /** * 记录日志,提供公共代码 * @author shkstart * @create 2019-11-09 15:25 */ @Component("logger") @Aspect //表示当前类是一个切面类 public class logger { //表达式 @Pointcut("execution(* cn.mepu.service.imp.*.*(..))") private void pt1(){ } /** * 前置日志 */ @Before("pt1()") public void beforePrintLog(){ System.out.println(" logger中前置开始记录日志" ); } /** * 后置日志 */ @AfterReturning("pt1()") public void afterreturningPrintLog(){ System.out.println(" logger中后置开始记录日志" ); } /** * 异常日志 */ @AfterThrowing("pt1()") public void afterThrowingPrintLog(){ System.out.println(" logger中异常开始记录日志" ); } /** * 最终日志 */ @After("pt1()") public void afterPrintLog(){ System.out.println(" logger中最终开始记录日志" ); } //@Around("pt1()") public Object aroundPringLog(ProceedingJoinPoint pjp){ Object reValue = null; try { Object[] args = pjp.getArgs();//得到方法所需的参数 //前置通知 System.out.println("logger中环绕通知开始记录日志前置" ); pjp.proceed(args);//明确调用切入点方法 //后置通知 System.out.println("logger中环绕通知开始记录日志后置" ); return reValue; } catch (Throwable throwable) { //异常通知 System.out.println("logger中环绕通知开始记录日志异常" ); throw new RuntimeException(throwable); }finally { //最终通知 System.out.println("logger中环绕通知开始记录日志最终" ); } } }
service层:
package cn.mepu.service.imp; import cn.mepu.service.IAccountService; import org.springframework.stereotype.Service; /** * 账户业务层实现类 * @author shkstart * @create 2019-11-09 15:23 */ @Service("accountService") public class AccountServiceImp implements IAccountService { @Override public void saveAccount() { System.out.println("保存"); } @Override public void updateAccount(int i) { System.out.println("更新"+i); } @Override public int deleteAccount() { System.out.println("删除"); return 0; } }
测试类:
package cn.mepu.Test; import cn.mepu.service.IAccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 测试Aop * @author shkstart * @create 2019-11-09 15:59 */ public class AopTest { public static void main(String[] args) { //1.获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("Bean.xml"); //2.获取对象 IAccountService as = (IAccountService) ac.getBean("accountService"); //3.执行方法 as.saveAccount(); } }
Bea文件:
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置spring创建容器时要扫描的包--> <context:component-scan base-package="cn.mepu"></context:component-scan> <!-- 配置spring开启注解AOP的支持 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
POM文件;
<?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>cn.mepu</groupId> <artifactId>day05_spring_aop_annotation</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency> </dependencies> </project>