码迷,mamicode.com
首页 > 编程语言 > 详细

Spring AOP @Around @Before @After 区别

时间:2017-10-17 15:12:21      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:lang   ota   bean   ntc   text   users   public   style   context   

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

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();
    }
}
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);
    }
}
<?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>

代码demo SVN  svn://gitee.com/rainyn/SSM

参考转自 http://outofmemory.cn/code-snippet/3025/spring-AOP-Around-Before-After-differentiate

 

Spring AOP @Around @Before @After 区别

标签:lang   ota   bean   ntc   text   users   public   style   context   

原文地址:http://www.cnblogs.com/thiaoqueen/p/7680826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!