AOP(Aspect Oriented Programming):面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
Spring作用:提供声明式事务;允许用户自定义切面
相关名词:
横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 …
切面(ASPECT):横切关注点被模块化的特殊对象。即,它是一个类。
通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
目标(Target):被通知对象。
代理(Proxy):向目标对象应用通知之后创建的对象。
切入点(PointCut):切面通知 执行的 “地点”的定义。
连接点(JointPoint):与切入点匹配的执行点。
SpringAOP中,通过Advice定义横切逻辑,Spring中支持5种类型的Advice:
Aop 在 不改变原有代码的情况下 , 去增加新的功能 ,有三种方式
方式一:Spring API实现
1 userService接口
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
2 userServiceImpl类
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加了一个用户");
}
public void delete() {
System.out.println("删除了一个用户");
}
public void update() {
System.out.println("修改了一个用户");
}
public void query() {
System.out.println("查询了一个用户");
}
}
3 Log
public class Log implements MethodBeforeAdvice{
/*
* target:目标对象
* method:要执行的目标对象的方法
* args:参数*/
public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
4 AfterLog
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(@Nullable Object returnValue, Method method, Object[] args, @Nullable Object target) throws Throwable {
System.out.println(method.getName()+"被执行了,返回值为"+returnValue);
}
}
5 ApplicationContext.xml
<?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
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 id="userservice" class="com.zhu.service.UserServiceImpl"/>
<bean id="log" class="com.zhu.log.Log"/>
<bean id="aftelog" class="com.zhu.log.AfterLog"/>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.zhu.service.UserServiceImpl.*(..))"/>
<!--advice-ref:引用自哪里
pointcut-ref:切入点-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="aftelog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
6 测试
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
//动态代理的是接口
UserService userservice = context.getBean("userservice", UserService.class);
userservice.add();
}
}
方式二:自定义类来实现Aop
1 自定义类DiyPointcut
public class DiyPointcut {
public void before(){
System.out.println("---------方法执行前---------");
}
public void after(){
System.out.println("---------方法执行后---------");
}
}
2 配置xml文件
<!--第二种方式-->
<!--注册bean-->
<bean id="diy" class="com.zhu.diy.DiyPointcut"/>
<!--aop的配置-->
<aop:config>
<!--第二种方式:使用AOP的标签实现-->
<aop:aspect ref="diy">
<aop:pointcut id="diyPonitcut" expression="execution(* com.zhu.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="diyPonitcut"/>
<aop:after pointcut-ref="diyPonitcut" method="after"/>
</aop:aspect>
</aop:config>
3 测试结果
方式三:使用注解实现
1 定义AnnotationPointcut类
@Aspect
public class AnnotationPointcut {
@Before("execution(* com.zhu.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("---------方法执行前---------");
}
@After("execution(* com.zhu.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("---------方法执行后---------");
}
@Around("execution(* com.zhu.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
System.out.println("签名:"+jp.getSignature());
//执行目标方法proceed
Object proceed = jp.proceed();
System.out.println("环绕后");
System.out.println(proceed);
}
}
2 配置
<bean id="annotationPointcut" class="com.zhu.annation.AnnotationPointcut"/>
<aop:aspectj-autoproxy/>
3 测试