• SpringAOP学习第一天 @Pointcut注解


    自从上班之后,就很少再看AOP相关的内容,几年时间里虽然也有一两次完整看过,一直没有机会用到,都忘记了。今天重温一下

    TestNG测试类

    package com.test.spring.aop.mineunderstatnd1;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
    import org.testng.annotations.Test;
    
    @ContextConfiguration(classes={AppConfig.class})
    public class RechargeTest extends AbstractTestNGSpringContextTests{
        
        @Autowired
        private AccountRecharge accountRecharge;
    
        @Autowired
        private Interceptor interceptor;
    
        @Test
        public void test() {
            Map request = new HashMap();
            accountRecharge.execute(request);
        }
    }

    业务类

    package com.test.spring.aop.mineunderstatnd1;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
    import org.testng.annotations.Test;
    
    @ContextConfiguration(classes={AppConfig.class})
    public class RechargeTest extends AbstractTestNGSpringContextTests{
        
        @Autowired
        private AccountRecharge accountRecharge;
    
        @Autowired
        private Interceptor interceptor;
    
        @Test
        public void test() {
            Map request = new HashMap();
            accountRecharge.execute(request);
        }
    }

    切面类

    @Aspect
    public class Interceptor {
        @Pointcut("execution(* com.test.spring.aop.mineunderstatnd1.AccountRecharge.execute(java.util.Map)) && args(request)")
        public void execute(HashMap request){}
        @Before("execute(request)")
        public void before(HashMap request) {
            System.out.println("前置拦截器");
        }
    }

    配置类

    package com.test.spring.aop.mineunderstatnd1;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    
    @Configuration
    @EnableAspectJAutoProxy
    @ComponentScan
    public class AppConfig {
        @Bean
        public AccountRecharge AccountRecharge() {
            return new AccountRecharge();
        }
        @Bean
        public Interceptor interceptor() {
            return new Interceptor();
        }
    }

    运行结果:

    前置拦截器
    do recharge
    PASSED: test
  • 相关阅读:
    C# 全局变量
    [C#]续:利用键代码自动转换生成字母键或其它键信息
    [WPF](小结2)DataGrid嵌套DataGrid(也叫主从表)
    [C#]winform窗口托盘
    C# arrayList动态添加对象元素,并取出对象元素的方法
    [WPF](小结3)DataGridInTreeView树嵌表
    [WPF](小结4)TreeView的数据分层模板
    [WPF](小结1)ListBox嵌套ListBox
    [C#]利用键代码自动转换生成字母键或其它键信息
    [C#]使用API 获取设置系统热键和快捷键
  • 原文地址:https://www.cnblogs.com/heben/p/7277446.html
Copyright © 2020-2023  润新知