一、AOP是什么?
AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。
AOP核心概念
- 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等…
- 切面(ASPECT):横切关注点被模块化的特殊对象。即,它是一个类。
- 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
- 目标(Target):被通知对象。
- 代理(Proxy) :向目标对象应用通知之后创建的对象。
- 切入点(Pointcut) :切面通知执行的“地点"的定义
- 连接点(JointPoint):与切入点匹配的执行点。
二、代码
1.需要的类
业务的接口
package com.shan.demo04;
public interface UserService {
void add();
void delete();
void update();
void query();
}
业务的实现类
package com.shan.demo02;
//改变业务代码在公司是大忌,因为你改动代码可能让整个项目崩溃
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("增加了一个用户");
}
@Override
public void delete() {
System.out.println("删除了一个用户");
}
@Override
public void update() {
System.out.println("修改了一个用户");
}
@Override
public void query() {
System.out.println("查询了一个用户");
}
}
前置日志类
package com.shan.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class BeforeLog implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
// Object[]:参数
// target:目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了!");
}
}
后置日志类
package com.shan.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
//returnValue:返回值
//method:要执行的目标对象的方法
// Object[]:参数
// target:目标对象
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了!"+target.getClass().getName()+"的"+method.getName()+"方法.返回结果为:"+returnValue);
}
}
自定义切入点
package com.shan.diy;
public class DiyPointCut {
public void before(){
System.out.println("=================方法执行前==================");
}
public void after(){
System.out.println("=================方法执行后==================");
}
}
注解实现AOP
package com.shan.diy;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect //标注这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* com.shan.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("=====方法执行前=====");
}
@After("execution(* com.shan.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("=====方法执行后=====");
}
}
Spring xml配置文件
<?xml version="1.0" encoding="UTF8" ?>
<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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.shan.service.UserServiceImpl"/>
<bean id="beforeLog" class="com.shan.log.BeforeLog"/>
<bean id="afterLog" class="com.shan.log.AfterLog"/>
<!--配置aop方式2,自定义类-->
<bean id="diy" class="com.shan.diy.DiyPointCut"/>
<!--3,注解-->
<bean id="annotation" class="com.shan.diy.AnnotationPointCut"/>
<!--方式3,开启注解支持-->
<aop:aspectj-autoproxy/>
<!--配置aop方式2,自定义类-->
<aop:config>
<!--自定义切面,ref:要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.shan.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
<aop:config>
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.shan.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
2.测试
import com.shan.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void diyPointCutTest(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理 代理的是接口
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
@Test
public void annotationPointCutTest(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理 代理的是接口
UserService userService = context.getBean("userService",UserService.class);
userService.add();
}
}
> =================方法执行前==================
> com.shan.service.UserServiceImpl的add被执行了!
> =====方法执行前=====
> 增加了一个用户
> =====方法执行后=====
> 执行了!com.shan.service.UserServiceImpl的add方法.返回结果为:null
> =================方法执行后==================
总结
AOP的实现方式:
- 默认的执行环绕
- 通过AOPconfig配置自定义的切入面,切入点,引用的类
- 注解实现AOP
对比有优先级,哪个config在上面,before就最先执行,after就最后执行.这两种方式可以同时存在,个人发现对比有不同!
作者有话说
博客创作不易,希望看到这里的读者动动你的小手点个赞,如果喜欢的小伙伴可以一键三连,作者大大在这里给大家谢谢了。