spring AOP @Around @Before @After 区别

本文介绍了Spring AOP中三种通知类型:@Around、@Before和@After的使用和区别。@Before在方法执行前运行,@After在方法执行后运行,无论方法是否正常结束,而@Around则可以控制方法的整个执行流程,包括决定是否执行目标方法以及在执行前后进行额外操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分享到: 
收藏 +22
2
此段小代码演示了spring aop中@Around @Before @After三个注解的区别
@Before是在所拦截方法执行之前执行一段逻辑。
@After 是在所拦截方法执行之后执行一段逻辑。
@Around是可以同时在所拦截方法的前后执行一段逻辑。

1. [代码][Java]代码     跳至 [1] [2] [3] [全屏预览]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.itsoft.action;
  import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Controller;
  /**
 
  * @author zxf
  * 演示aop测试类
  */ @Controller public class UserAction {
      
     public void queryUsers(){
          
         System.out.println( "查询所有用户【all users list】" );
     }
      
     public static void main(String[] args) {
          
         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( "application-aop.xml" );
          
         UserAction userAction = (UserAction)ctx.getBean( "userAction" );
         userAction.queryUsers();
          
         ctx.destroy();
     }
}
 
 

2. [代码][Java]代码     跳至 [1] [2] [3] [全屏预览]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.itsoft;
  import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component;
  /**
 
  * @author Administrator
  * 通过aop拦截后执行具体操作
  */ @Aspect @Component public class LogIntercept {
      
     @Pointcut ( "execution(public * com.itsoft.action..*.*(..))" )
     public void recordLog(){}
      
      
     @Before ( "recordLog()" )
     public void before() {
         this .printLog( "已经记录下操作日志@Before 方法执行前" );
     }
      
     @Around ( "recordLog()" )
     public void around(ProceedingJoinPoint pjp) throws Throwable{
         this .printLog( "已经记录下操作日志@Around 方法执行前" );
         pjp.proceed();
         this .printLog( "已经记录下操作日志@Around 方法执行后" );
     }
      
      
     @After ( "recordLog()" )
     public void after() {
         this .printLog( "已经记录下操作日志@After 方法执行后" );
     }
      
      
     private void printLog(String str){
         System.out.println(str);
     }
}

3. [代码]演示@Around @Before @After 区别     跳至[1][2][3][全屏预览]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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"
       xmlns:context= "http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http: //www.springframework.org/schema/context       http: //www.springframework.org/schema/context/spring-context-3.0.xsd       http: //www.springframework.org/schema/beans        http: //www.springframework.org/schema/beans/spring-beans-3.0.xsd       http: //www.springframework.org/schema/aop        http: //www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <context:annotation-config />
     <context:component-scan base- package = "com.itsoft" />
     <aop:aspectj-autoproxy />
</beans>


开源中国-程序员在线工具:Git代码托管API文档大全(120+) JS在线编辑演示 二维码 更多»

 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值