Spring实战+总结归纳:AOP导学图+实例

一、思维导图

Spring实战+总结归纳:AOP导学图+实例_第1张图片

二、构建AOP步骤

1.导包:AOP,Aspect

        
			org.springframework
			spring-aop
			${spring.version}
		
		
		
			org.springframework
			spring-aspects
			${spring.version}
		

2.在Java配置文件中开启代理

@EnableAspectJAutoProxy

3.编写切面(包括切点和通知)

package com.yan.otl.main.aspect;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LoginControllerAspect {
	
	@Pointcut("execution(* com.yan.otl.main.controller.LoginController.getUser(..))")
	public void getUser() {}
	
	@Before("getUser()")
	public void before() {
		System.out.println("---------------------前置通知");
	}
	
	@After("getUser()")
	public void after() {
		System.out.println("---------------------后置通知,返回通知和异常通知");
	}
	
	@AfterReturning("getUser()")
	public void afterReturning() {
		System.out.println("---------------------返回通知");
	}
	
	@AfterThrowing("getUser()")
	public void afterThrowing() {
		System.out.println("---------------------异常通知");
	}
}

4、输出结果

---------------------前置通知
com.yan.otl.main.data.User@6ad075e1
---------------------后置通知,返回通知和异常通知
---------------------返回通知

 

你可能感兴趣的:(Java,Spring)