编写核心业务代码(目标类的目标方法)
编写切面类,切面类中有通知(增强功能方法)
在配置文件中,配置织入关系,即将哪些通知与哪些连接点进行结合
Spring 框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。
在 spring 中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式。
aop:面向切面编程
aop底层实现:基于JDK的动态代理 和 基于Cglib的动态代理
aop的重点概念:
Pointcut(切入点):被增强的方法Advice(通知/ 增强):封装增强业务逻辑的方法Aspect(切面):切点+通知Weaving(织入):将切点与通知结合的过程
开发明确事项:
谁是切点(切点表达式配置)谁是通知(切面类中的增强方法)将切点和通知进行织入配置
①导入 AOP 相关坐标
②创建目标接口和目标类(内部有切点)
③创建切面类(内部有增强方法)
④将目标类和切面类的对象创建权交给 spring
⑤在 applicationContext.xml 中配置织入关系
⑥测试代码
①导入 AOP 相关坐标
org.springframework
spring-context
5.0.5.RELEASE
org.aspectj
aspectjweaver
1.8.13
②创建目标接口和目标类(内部有切点)
//接口
public interface TargetInterface {public void method();
}//实现类
public class Target implements TargetInterface {@Overridepublic void method() {System.out.println("Target running....");}
}
③创建切面类(内部有增强方法)
public class MyAspect {//前置增强方法public void before(){System.out.println("前置代码增强.....");}
}
④将目标类和切面类的对象创建权交给 spring管理
⑤在 applicationContext.xml 中配置织入关系
导入aop命名空间
⑤在 applicationContext.xml 中配置织入关系
配置切点表达式和前置增强的织入关系
⑥测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {@Autowiredprivate TargetInterface target;@Testpublic void test1(){target.method();}
}
表达式语法:
execution([修饰符] 返回值类型 包名.类名.方法名(参数))
访问修饰符可以省略
返回值类型、包名、类名、方法名可以使用星号* 代表任意
包名与类名之间一个点 . 代表当前包下的类,两个点 … 表示当前包及其子包下的类
参数列表可以使用两个点 … 表示任意个数,任意类型的参数列表
例如:
execution(public void com.itheima.aop.Target.method()) //不常用,具体的方法,限制了
execution(void com.itheima.aop.Target.*(..)) //具体的类下的所有方法都会被增强,无返回值execution(* com.itheima.aop.*.*(..)) //常用(推荐),该包下的任意类的任意方法(参数任意)都会被增强,返回值任意
execution(* com.itheima.aop..*.*(..)) //aop下及其子包下的任意类任意方法都会被增强
execution(* *..*.*(..)) //全部都任意
通知的配置语法:

Java类:
//增强对象
public class MyAspect {//后置增强public void before() {System.out.println("前置增强................................");}//后置增强public void afterReturning() {System.out.println("后置增强................................");}//Proceeding JoinPoint 正在执行的链接点----切点//环绕增强public Object Around(ProceedingJoinPoint point) throws Throwable {System.out.println("环绕前增强................................");Object proceed = point.proceed();//切点方法System.out.println("环绕后增强................................");return proceed;}//异常增强public void afterThrowing() {System.out.println("异常增强................................");}//最终增强public void after() {System.out.println("最终增强................................");}
}
xml:
当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽取后的切点表达式。
//切点表达式
execution([修饰符] 返回值类型 包名.类名.方法名(参数))
基于注解的aop开发步骤:
①创建目标接口和目标类(内部有切点)
②创建切面类(内部有增强方法)
③将目标类和切面类的对象创建权交给 spring
④在切面类中使用注解配置织入关系
⑤在配置文件中开启组件扫描和 AOP 的自动代理
⑥测试
①创建目标接口和目标类(内部有切点)
public interface TargetInterface {public void method();
}public class Target implements TargetInterface {@Overridepublic void method() {System.out.println("Target running....");}
}
②创建切面类(内部有增强方法)
//切面类
public class MyAspect {//前置增强方法public void before(){System.out.println("前置代码增强.....");}
}
③将目标类和切面类的对象创建权交给 spring
//实现目标接口
@Component("target")
public class Target implements TargetInterface {@Overridepublic void method() {System.out.println("Target running....");}
}
@Component("myAspect")
public class MyAspect {public void before(){System.out.println("前置代码增强.....");}
}
④在切面类中使用注解配置织入关系
//注解配置切面类,配置织入
@Component("myAspect")
@Aspect
public class MyAspect {@Before("execution(* com.itheima.aop.*.*(..))") //前置增强public void before(){System.out.println("前置代码增强.....");}
}
⑤在配置文件中开启组件扫描和 AOP 的自动代理
⑥测试代码
//测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {@Autowiredprivate TargetInterface target;@Testpublic void test1(){target.method();}
}
通知的配置语法:@通知注解(“切点表达式")

同 xml配置
aop 一样,我们可以将切点表达式抽取。抽取方式是在切面内定义方法,在该方法上使用@Pointcut注解定义切点表达式,然后在在增强注解中进行引用。具体如下:
@@Component("myAspect")
@Aspect
public class MyAspect {@Before("MyAspect.myPoint()")public void before(){System.out.println("前置代码增强.....");}@Pointcut("execution(* com.itheima.aop.*.*(..))")public void myPoint(){}
}
①使用@Aspect标注切面类
②使用@通知注解标注通知方法
③在配置文件中配置aop自动代理 aop:aspectj-autoproxy/

提供声明式事务;允许用户自定义切面
【重点】使用AOP织入,需要导入一依赖包!
org.aspectj aspectjweaver 1.9.4
service
public interface UserService {public void add();public void delete();public void update();public void select();
}
public class UserServiceImpl implements UserService{@Overridepublic void add() {System.out.println("增加了一个用户");}@Overridepublic void delete() {System.out.println("删除了一个用户");}@Overridepublic void update() {System.out.println("更新了一个用户");}@Overridepublic void select() {System.out.println("查询了一个用户");}
}
spring配置——applicationContext.xml
test
import com.blue.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//动态代理代理的是接口UserService userService = context.getBean("userService", UserService.class);userService.add();}
}
public class DiyPointCut {public void before(){System.out.println("=============方法执行前===========");}public void after(){System.out.println("=============方法执行后===========");}
}
AnnotationPointCut
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;@Aspect
public class AnnotationPointCut {@Before("execution(* com.blue.service.UserServiceImpl.*(..))")public void before(){System.out.println("=============方法执行前===========");}@After("execution(* com.blue.service.UserServiceImpl.*(..))")public void after(){System.out.println("=============方法执行后===========");}}