zoukankan      html  css  js  c++  java
  • Spring AOP:@Before、@After 的 JavaConfig 写法

    网络上关于Spring AOP的范例大都是使用xml作配置文件,见此特地写一些JavaConfig的范例,既为加深理解,亦为加强记忆。如需引用或转载的同学,请注明来源。

    使用Spring AOP,要成功运行起代码,只用Spring提供给开发者的jar包是不够的,请额外上网下载两个jar包导入项目中:

    • aopalliance.jar
    • aspectjweaver.jar。

    由于我使用IDEA_U创建的spring项目,aopalliance.jar是Maven自动下载的,而 aspectjweaver.jar 则需要另外下载aspectj.jar,然后用解压软件打开,解压出 aspectjweaver.jar

    先写一个接口:

    @Component
    public interface Person {
        void say();
        void run();
    }

    再写两个实现类:

    @Component
    @Qualifier("adults")
    public class Adults implements Person {
    
        private String classname = "adults";
    
        @Override
        public void say() {
            System.out.println("I am " + classname + " , I like acid rock.");
        }
    
        @Override
        public void run() {
            System.out.println("I am " + classname + " , I like long-distance");
        }
    }
    @Component
    @Qualifier("children")
    public class Children implements Person {
    
        private String classname = "children";
    
        @Override
        public void say() {
            System.out.println("We are " + classname + " , we like nursery rhymes.");
        }
    
        @Override
        public void run() {
            System.out.println("We are " + classname + " , we like running around.");
        }
    }

    然后写一个切面,这也是一个类:

    @Component
    @Aspect //声明这是一个切面。必须的!
    public class AspectConfig {
    
        //通知和切入点的混合写法;
        //第一个 * 号表示任意返回类型,第二个 * 号表示Person的所有方法
        @Before("execution(* com.san.spring.aop.Person.*(..))")
        public void showTime1(){
            System.out.println("CurrentTime = " + System.currentTimeMillis());
        }
    
        @After("execution(* com.san.spring.aop.Person.*(..))")
        public void showTime2(){
            System.out.println("CurrentTime = " + System.currentTimeMillis());
        }
    }

    上面的切面也可以这样定义

    @Component
    @Aspect //声明这是一个切面。必须的! 
    public class AspectConfig {
    
        // 定义一个切点
        @Pointcut("execution(* com.san.spring.aop.Person.*(..))")
        public void pointcut(){}
    
        // 定义通知
        @Before("pointcut()")
        public void showTime1(){
            System.out.println("CurrentTime = " + System.currentTimeMillis());
        }
    
        // 定义通知
        @After("pointcut()")
        public void showTime2(){
            System.out.println("CurrentTime = " + System.currentTimeMillis());
        }
    }

    定义spring的JavaConfig类:

    @Configuration
    @ComponentScan
    @EnableAspectJAutoProxy //启用自动代理功能。必须的!
    public class SpringConfig {
    }

    写一个测试类:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = SpringConfig.class)
    public class AopTest {
    
        @Autowired
        @Qualifier("children")
        private Person person;
    
        @Test
        public void testMethod(){
            person.say();
            person.run();
        }
    }

    本文参考了http://www.cnblogs.com/xrq730/p/4919025.html

  • 相关阅读:
    自定义注解!绝对是程序员装逼的利器!!
    mybatis连接数据库错误解决方法
    SQL基础
    【2021-1-5】QT+SQLsever数据库的数据管理系统
    以友盟+U-Push为例,深度解读消息推送的筛选架构解决方案应用与实践
    基于Linux的MySQL基本操作
    SQL server函数转Oracle问题之一,强行使用临时表
    安装 部署 postgresql数据库 搭建主从节点 (业务库)
    SQL练习题一(逐行累计)
    ThinkPHP中,display和assign用法详解
  • 原文地址:https://www.cnblogs.com/xxdsan/p/6399711.html
Copyright © 2011-2022 走看看