- 一、AOP在Spring中的作用
- 二、使用Spring实现AOP
- 方式一:使用Spring的API接口
- 方法二:自定义类来实现AOP(主要是切面定义)
- 方式三: 使用注解实现
- 三、AOP优点
概念
AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP 是 OOP 的延续,是软件开发中的一个热点,也是 Spring 框架中的一个重要内容,是函数式编程的一种衍生范型。利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率
可以通过预编译方式
和运行其动态代理
实现在不修改源代码的情况下给程序动态统一添加某种特定功能,对我们的已有方法进行增强的一种技术。AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,提高代码的灵活性和可扩展性,AOP可以说也是这种目标的一种实现
提供声明式事务;允许用户自定义切面
以下名词需要了解下:
-
横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存 ,事务等等 ….
-
切面(ASPECT):横切关注点被模块化的特殊对象。即,它是一个类,封装的、用于横向插入系统的功能类
-
通知 / 增强处理(Advice):切面必须要完成的工作。即,它是类中的一个方法,它是切面的具体实现,可以通知 / 增强处理
-
目标(Target):被通知对象
-
代理(Proxy):向目标对象应用通知之后,程序动态创建的对象
-
连接点(JointPoint):与切入点匹配的执行点,是程序执行过程中某个特定的节点
-
切入点(PointCut):切面通知执行的
地点
的定义。在某个连接点满足预先指定的条件时, AOP 就能够定位到这个连接点,在连接点处插入切面,该连接点也就变成了切入点 -
引介(Introduction):引介是一种特殊的通知,它可为目标对象添加一些属性和方法
-
织入(Weaving):是将切面应用到目标对象的过程,这个过程可以是在编译时(例如使用 AspectJ 编译器),类加载时,运行时完成。Spring AOP 和其它纯 Java AOP 框架一样,是在运行时执行植入
SpringAOP中,通过Advice定义横切逻辑,Spring中支持5种类型的Advice
通知类型 | 连接点 | 实现接口 |
---|---|---|
前置通知 | 方法前 | org.springframework.aop.MethodBeforeAdvice |
后置通知 | 方法后 | org.springframework.aop.AfterReturningAdvice |
环绕通知 | 方法前后 | org.aopalliance.intercept.MethodInterceptor |
异常抛出通知 | 方法抛出异常 | org.springframework.aop.ThrowsAdvice |
引介通知 | 类中增加新的方法属性 | org.springframework.aop.IntroductionInterceptor |
即 Aop 在 不改变原有代码的情况下,去增加新的功能 .
二、使用Spring实现AOP【重点】使用AOP织入,需要到一个依赖包
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.8version>
dependency>
方式一:使用Spring的API接口
AfterLog.class
public class AfterLog implements AfterReturningAdvice {
//returnValue: 返回值
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为"+returnValue);
}
}
Log.class
public class Log implements MethodBeforeAdvice {
//method: 要执行的目标对象方法
//args: 参数
//target: 目标对象
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
接口和实现类
applicationContext.xml配置
<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="com.vinjcent.service.UserServiceImpl"/>
<bean id="log" class="com.vinjcent.log.Log"/>
<bean id="afterLog" class="com.vinjcent.log.AfterLog"/>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.vinjcent.service.UserServiceImpl.*(..))"/>
<aop:pointcut id="pointcut2" expression="execution(* com.vinjcent.service.UserServiceImpl.*(..))"/>
<aop:pointcut id="pointcut3" expression="execution(* com.vinjcent.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
aop:config>
beans>
文件目录
测试
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
/**
* getBean方法 第二个参数应该写的是接口,而不是它的实现类
* 代理类与被代理类都是VisitService接口的实现类
* 新生成的代理类只能向上转型为接口类型,不能强转给同等级VisitServiceImpl
* 动态代理的是接口【使用框架都要用接口类型来引用实现类对象】
*/
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
}
方法二:自定义类来实现AOP(主要是切面定义)
自定义一个类DiyPointCut.class
public class DiyPointCut {
public void pointCut(){
//配置切点
}
public void before(){
System.out.print("方法执行前");
}
public void after(){
System.out.println("方法执行后");
}
public void afterReturning(){
System.out.println("方法返回后");
}
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前");
//执行方法
Object proceed = joinPoint.proceed();
System.out.println("环绕后");
}
public void afterThrowing(){
System.out.println("方法抛出异常");
}
}
applicationContext.xml配置
<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="com.vinjcent.service.UserServiceImpl"/>
<bean id="div" class="com.vinjcent.diy.DiyPointcut"/>
<aop:config>
<aop:aspect ref="div">
<aop:pointcut id="point" expression="execution(* com.vinjcent.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
<aop:after-returning method="afterReturning" pointcut-ref="point"/>
<aop:around method="around" pointcut-ref="point"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="point"/>
aop:aspect>
aop:config>
方式三: 使用注解实现
在DiyPointCut.class 类中添加注解
@Aspect
@Component
public class DiyPointCut {
/**
* 由于spring从5.2.6.RELEASE版本之后
* AOP的执行顺序出现了问题 AfterReturning 在 After之前进行了执行
* 而我用的是5.3.19版本
*/
@Pointcut("execution(* com.vinjcent.service.UserServiceImpl.*(..))")
public void pointCut(){
//配置切点
}
@Before("pointCut()")
public void before(){
System.out.print("方法执行前");
}
@Before("pointCut()")
public void after(){
System.out.println("方法执行后");
}
@AfterReturning("pointCut()")
public void afterReturning(){
System.out.println("方法返回后");
}
@Around("pointCut()")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前");
//执行方法
Object proceed = joinPoint.proceed();
System.out.println("环绕后");
}
@AfterThrowing("pointCut()")
public void afterThrowing(){
System.out.println("方法抛出异常");
}
}
applicationContext.xml配置
<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="com.vinjcent.service.UserServiceImpl"/>
<bean id="annotationPointcut" class="com.vinjcent.diy.DiyPointCut"/>
<aop:aspectj-autoproxy/>
三、AOP优点
-
业务代码更加简洁,例如当需要在业务行为前后做一些事情时候,只需要在该行为前后配置切面进行处理,无须修改业务行为代码,这也大大降低业务逻辑各部分之间的耦合度
-
切面逻辑封装性好,并且可以被复用,例如我们可以把打日志的逻辑封装为一个切面,那么我们就可以在多个相关或者不相关的类的多个方法上配置该切面。提高了程序的可重用性,同时也提高了开发的效率
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)