1.创建目标类
public class User {
public void add(){
System.out.println("add++++++++++");
}
}
2.创建增强类(编写增强逻辑)
public class UserProxy {
public void before(){
System.out.println("before +++++++++++");
}
}
3.配置通知
3.1 配置文件中开启注解扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启组件扫描-->
<context:component-scan base-package="com.aop"></context:component-scan>
</beans>
3.2 使用注解创建目标对象
@Component
public class User {
public void add(){
System.out.println("add++++++++++");
}
}
@Component
@Aspect
public class UserProxy {
public void before(){
System.out.println("before +++++++++++");
}
}
3.3 在增强类上使用@Aspect注解
3.4 在配置文件开启代理(会自动扫描带有@Aspect注解的类,自动生成代理对象)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启组件扫描-->
<context:component-scan base-package="com.aop"></context:component-scan>
<!--开启Aspectj代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
4.配置不同类型的通知
@Component
@Aspect
public class UserProxy {
//前置通知
@Before("execution(* com.aop.User.add(..))")
public void before(){
System.out.println("前置通知");
}
//最终通知:是在方法之后执行
@After("execution(* com.aop.User.add(..))")
public void after(){
System.out.println("后置通知 +++++++++++");
}
//后置通知:(返回通知):在方法返回结果以后执行。
@AfterReturning("execution(* com.aop.User.add(..))")
public void afterReturning(){
System.out.println("返回通知(最终通知) +++++++++++");
}
//错误通知:
@AfterThrowing("execution(* com.aop.User.add(..))")
public void afterThrowing(){
System.out.println("错误通知 +++++++++++");
}
//环绕通知
@Around("execution(* com.aop.User.add(..))")
public void around(ProceedingJoinPoint point) throws Throwable {
System.out.println("环绕通知之前");
point.proceed();
System.out.println("环绕通知之后");
}
}
5.抽取共同的切入点。
@Component
@Aspect
public class UserProxy {
//当所有的通知都使用共同的切入点的话,可以提取出来
@Pointcut("execution(* com.aop.User.add(..))")
public void pointCut(){
}
//前置通知
@Before("pointCut()")
public void before(){
System.out.println("前置通知");
}
//最终通知:是在方法之后执行
@After("pointCut()")
public void after(){
System.out.println("后置通知 +++++++++++");
}
//后置通知:(返回通知):在方法返回结果以后执行。
@AfterReturning("pointCut()")
public void afterReturning(){
System.out.println("返回通知(最终通知) +++++++++++");
}
//错误通知:
@AfterThrowing("pointCut()")
public void afterThrowing(){
System.out.println("错误通知 +++++++++++");
}
//环绕通知
@Around("pointCut()")
public void around(ProceedingJoinPoint point) throws Throwable {
System.out.println("环绕通知之前");
point.proceed();
System.out.println("环绕通知之后");
}
}
6对于同一个方法有同时存在多个增强类时,通过@Order(int),设置优先级,数值越小,优先级越高。
@Component
@Aspect
@Order(1)
7.完全注解开发(不需要xml配置文件)
package com.aop;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan(basePackages = {"com.aop"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class Config {
}