zoukankan      html  css  js  c++  java
  • spring AOP注解

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

    [Java]代码

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

    [Java]代码

    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);
        }
    }
  • 相关阅读:
    Command模式应用实践
    .Net中的设计模式——Strategy模式
    PetShop之ASP.NET缓存
    征求书名
    PetShop之业务逻辑层设计
    Buider模式应用实践
    公告:目前博客园书业出版小组的工作进度
    “AS3.0高级动画编程”学习:第二章转向行为(下)
    as3: this,stage,root的测试
    As3.0中的位图(Bitmap/BitmapData)编程
  • 原文地址:https://www.cnblogs.com/moxiaotao/p/9629107.html
Copyright © 2011-2022 走看看