AOP术语
为什么需要AOP
AOP也就是面向切面编程,作为面向对象编程的一种补充,已经成为一种比较成熟的编程方式。其实AOP问世的时间并不太长,AOP和OOP互为补充,面向切面编程将程序运行过程分解成各个切面。
AOP专门用于处理系统中分布于各个模块(不同方法)中的交叉关注点的问题,在javaEE应用中,常常通过AOP来处理一些具有横切性质的系统级服务,如事务管理、安全检查、缓存、对象池管理等,AOP已经成为一种非常常用的解决方案。
使用AspectJ实现AOP
AspectJ是一个基于Java语言的AOP框架,提供了强大的AOP功能,其他很多AOP框架都借鉴或采纳其中的一些思想。其主要包括两个部分:一个部分定义了如何表达,定义了AOP编程中的语法规范,通过这套语法规范,可以方便地用AOP来解决Java语言中存在的交叉关注点的问题;另一个部分是工具部分,包括编译、调试工具。
AOP实现可分为两类:
- 静态AOP实现: AOP框架在编译阶段对程序进行修改,即实现对目标类的增强,生成静态的AOP代理类,以AspectJ为代表。
- 动态AOP实现: AOP框架在运行阶段动态生成AOP代理,以实现对目标对象的增强,以Spring AOP为代表。
一般来说,静态AOP实现具有较好的性能,但需要使用特殊的编译器。动态AOP实现是纯Java实现,因此无须特殊的编译器,但是通常性能略差。
AOP的基本概念
关于面向切面编程的一些术语:
- 切面(Aspect): 切面用于组织多个Advice,Advice放在切面中定义。
- 连接点(Joinpoint): 程序执行过程中明确的点,如方法的调用,或者异常的抛出。在Spring AOP中,连接点总是方法的调用。
- 增强处理(Advice): AOP框架在特定的切入点执行的增强处理。处理有"around"、"before"和"after"等类型
- 切入点(Pointcut): 可以插入增强处理的连接点。简而言之,当某个连接点满足指定要求时,该连接点将被添加增强处理,该连接点也就变成了切入点。
//aop的配置文件
<?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:context="http://www.springframework.org/schema/context"
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/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">
<!--
创建service包下的所有类
-->
<context:compontent-scan base-package="com.zhiyou.service"></context:compontent-scan>
<bean id="LoggingManager" class="com.zhiyou.aop.LoggingManager"></bean>
<!--aop配置
java的动态代理:只能对接接口
cglib动态代理
proxy-target-class:false 使用java动态代理
proxy-target-class:true 使用cglib动态代理
-->
<aop:config proxy-target-class="true">
<!--配置切点-->
<!--service包及子包下,所有以add开头-->
<aop:pointcut experssion="execution(* com.zhiyou.service..add*(..))" id="point1">
<!--配置切点-->
<!--service包及子包下,所有以delete开头-->
<aop:pointcut experssion="execution(* com.zhiyou.service..*.delete(..))"
<!--配置切面-->
<aop:aspect ref="loggingManager">
<!--配置通知
5种通知:
before:前置通知
after:后置通知
around:环绕通知
after-returning 返回值通知
after-throwing 抛出异常通知
-->
<aop:after method="writeLogging" pointcut-ref="point1">
<aop:before method="updateLogging" pointcut-ref="point1">
<aop:before method="updateLoging" pointcut-ref="point2"/>
</aop:aspect>
<!--
在执行com.zhiyou.service包下及其子包下任意一个以add开头的方法之后
就会执行LoggingManager类中的writeLoggin的方法
-->
</aop:config>
</beans>
使用注解对AOP进行配置
<?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:context="http://www.springframework.org/schema/context"
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/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">
<!--创建com.zhiyou包下的所有类-->
<context:component-scan base-package="com.zhiyou"></context:component-scan>
<!-- 使用注解的方式配置aop -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
package com.zhiyou.service.impl;
import org.springframework.stereotype.Service;
import com.zhiyou.service.MessageService;
//使用注解的方式创建service包下类的对象
@Service
public class MessageServiceImpl implements MessageService {
@Override
public void addMsg(String title) {
// TODO Auto-generated method stub
System.out.println("添加一条记录");
}
@Override
public void deleteMsg() {
// TODO Auto-generated method stub
System.out.println("删除一条记录");
}
@Override
public void queryMsg() {
// TODO Auto-generated method stub
System.out.println("查询记录");
}
}
package com.zhiyou.aop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component //配置bean对象
@Aspect //配置切面
public class LoggingManager {
//配置起点
@After("execution(* com.zhiyou.service..*.add*(..))")
public void addLogging(){
System.out.println("logging 。。添加日志");
}
@Before("pointCut1()")
public void updateLogging(){
System.out.println("logging 。。修改日志");
}
//声明切点表达式
//方法名作为表达式名称
@Pointcut("execution(* com.zhiyou.service..*.add*(..))")
public void pointCut1(){
}
}