linux怎么查看本机内存大小
234
2022-11-06
一篇文章带你了解Spring AOP 的注解
目录1、xml 的方式实现 AOP①、接口 UserService②、实现类 UserServiceImpl③、切面类,也就是通知类 MyAspect④、AOP配置文件 applicationContext.xml⑤、测试⑥、控制台打印结果2、注解实现 AOP①、导入相应的 jar 包,以及在 applicationContext.xml 文件中导入相应的命名空间。②、注解配置 beanxml配置:注解配置:③、配置扫描注解识别④、注解配置 AOP一、我们用xml配置过如下: 二、如何让 Spring 认识我们所配置的 AOP 注解?三、注解配置前置通知四、注解配置后置通知五、测试六、控制台打印结果 3、注解改进 4、总结通知切入点
1、xml 的方式实现 AOP
①、接口 UserService
package com.ys.aop;
public interface UserService {
//添加 user
public void addUser();
//删除 user
public void deleteUser();
}
②、实现类 UserServiceImpl
package com.ys.aop;
public class UserServiceImpl implements UserService{
@Override
public void addUser() {
System.out.println("增加 User");
}
@Override
public void deleteUser() {
System.out.println("删除 User");
}
}
③、切面类,也就是通知类 MyAspect
package com.ys.aop;
import org.aspectj.lang.JoinPoint;
public class MyAspect {
/**
* JoinPoint 能获取目标方法的一些基本信息
* @param joinPoint
*/
public void myBefore(JoinPoint joinPoint){
System.out.println("前置通知 : " + joinPoint.getSignature().getName());
}
public void myAfterReturning(JoinPoint joinPoint,Object ret){
System.out.println("后置通知 : " + joinPoint.getSignature().getName() + " , -->" + ret);
}
public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
System.out.println("抛出异常通知 : " + e.getMessage());
}
public void myAfter(){
System.out.println("最终通知");
}
}
④、AOP配置文件 applicationContext.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd">
⑤、测试
@Test
public void testAop(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService useService = (UserService) context.getBean("userService");
useService.addUser();
useService.deleteUser();
}
⑥、控制台打印结果
上面的例子很简单,就是在 UserService 的 addUser()方法和 deleteUser()方法增加前置通知和后置通知,这在实际操作中很好理解。比如这是和数据库打交道的话,那么我们在addUser() 或者deleteUser() 时,必须要在前面开始事务,操作完毕后提交事务。下面我们就用注解的方式来配置。
2、注解实现 AOP
①、导入相应的 jar 包,以及在 applicationContext.xml 文件中导入相应的命名空间。
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd">
②、注解配置 bean
xml配置:
注解配置:
目标类:
切面类:
③、配置扫描注解识别
这个我们在前面也讲过,上面配置的注解,Spring 如何才能识别这些类上添加了注解呢?我们必须告诉他。
在applicationContext.xml 文件中添加如下配置:
④、注解配置 AOP
一、我们用xml配置过如下:
这是告诉 Spring 哪个是切面类。下面我们用注解配置
我们在切面类上添加 @Aspect 注解,如下:
二、如何让 Spring 认识我们所配置的 AOP 注解?
光有前面的类注解扫描是不够的,这里我们要额外配置 AOP 注解识别。
我们在 applicationContext.xml 文件中增加如下配置:
三、注解配置前置通知
我们先看 xml 配置前置通知如下:
那么注解的方式如下:
四、注解配置后置通知
xml 配置后置通知:
注意看,后置通知有个 returning="ret" 配置,这是用来获得目标方法的返回值的。
注解配置如下:
五、测试
@Test
public void testAopAnnotation(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext_Annotation.xml");
UserService useService = (UserService) context.getBean("userService");
useService.addUser();
useService.deleteUser();
}
六、控制台打印结果
3、注解改进
我们可以看前置通知和后置通知的注解配置:
注意看红色框住的部分,很显然这里是重复的,而且如果我们有多个通知方法,那就得在每个方法名都写上该注解,而且如果包名够复杂,也很容易写错。那么怎么办呢?
解决办法就是声明公共切入点:
①、在 切面类 MyAspect.java 中新增一个切入点方http://法 myPointCut(),然后在这个方法上添加@Pointcut 注解
②、那么前置通知和后置通知,我们可以进行如下改写配置:
4、总结
上面我们只进行了前置通知和后置通知的讲解,还有比如最终通知、环绕通知、抛出异常通知等,配置方式都差不多,这里就不进行一一讲解了。然后我们看一下这些通知的注解:
@Aspect 声明切面,修饰切面类,从而获得 通知。
通知
@Before 前置@AfterReturning 后置@Around 环绕@AfterThrowing 抛出异常@After 最终
切入点
@PointCut ,修饰方法 private void xxx(){} 之后通过“方法名”获得切入点引用
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注我们的更多内容!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~