*[HTML]:
AOP在spring中的作用
横切关注点:跨越应用程序多个模块的方法或功能。
即是,于我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。
如日志,安全,缓存,事务等等
切面(ASPECT):横切关注点 被模块化的特殊对象,即,他是一个类。
通知(Advice):切面必须要完成的工作。即,他是类中的一个方法
目标(Target):被通知对象。
代理(Proxy):向目标对象应用通知之后创建的对象。
切入点(PointCut):切面通知执行的“地点”的意义。
链接垫(JointPoint):与切入点匹配的执行点。
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="xpu.Service.ServiceImpl"/>
<bean id="log" class="xpu.log.Log"/>
<bean id="afterLog" class="xpu.log.AfterLog"/>
方式一
原生spring API接口
配置application.xml
*表示所有返回类型,xpu.Service.ServiceImpl是全类名 . *表示该类下所有方法,(. .)表示任意参数
<aop:config>
<!--切入点 : expression:表达式,execution(要执行的位置)-->
<aop:pointcut id="pointcut" expression="execution(* xpu.Service.ServiceImpl.*(..))"/>
<!--执行环绕增加!-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
public class Log implements MethodBeforeAdvice {
//method: 要执行的目标对象的方法
//args:参数
//target:目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
public class AfterLog implements AfterReturningAdvice {
// returnValue 返回值
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法;返回结果为"+returnValue);
}
}
public interface Service {
public void add();
public void delete();
public void update();
public void select();
}
public class ServiceImpl implements Service {
public void add() {
System.out.println("增加了一个用户");}
public void delete() {
System.out.println("删除了一个用户");}
public void update() {
System.out.println("修改了一个用户");}
public void select() {
System.out.println("查询了一个用户");}
}
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("baens.xml");
//动态代理代理的是一个接口
Service userService = context.getBean("userService", Service.class);
userService.add();
userService.select();
}
}
方式二
自定义类
<bean id="diy" class="xpu.diy.DiyPointCut"/>
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="point" expression="execution(* xpu.Service.ServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
public class DiyPointCut {
public void before(){
System.out.println("==============");
}
public void after(){
System.out.println("==============");
}
}
方式三
使用注解
<bean id="annotationPointCut" class="xpu.diy.AnnotationPointCut"/>
<aop:aspectj-autoproxy/>
//使用注解方式实现AOP
@Aspect//标注这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* xpu.Service.ServiceImpl.*(..))")
public void before(){
System.out.println("=============方法执行前===========");
}
@After("execution(* xpu.Service.ServiceImpl.*(..))")
public void after(){
System.out.println("============方法执行后=============");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点;
@Around("execution(* xpu.Service.ServiceImpl.*(..))")
public void around(ProceedingJoinPoint p) throws Throwable {
System.out.println("环绕前");
//执行方法
Object proceed = p.proceed();
System.out.println("环绕后");
}
}