• spring学习笔记——面向切面编程AOP三


    前面介绍了aop的xml的简单配置和使用,下面介绍一下aop的注解使用方式的例子,可以对照第二篇的

    pom文件是相同的;

    spring.xml文件:

    <?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"
    xsi:schemaLocation="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">
    
        <aop:aspectj-autoproxy />
        <bean id = "audience" class="spring.study.spring.aop.Audience" />
        <bean id = "player" class = "spring.study.spring.aop.Player" />
        <bean id = "audienceAnnotation" class = "spring.study.spring.aop.AudienceByAnnotation" />
        
    </beans>

    java类:AudienceByAnnotation类中的song()方法只是一个标识,供@pointcut注解依附,它里面的内容并不重要

    package spring.study.spring.aop;
    
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    
    @Aspect
    public class AudienceByAnnotation {
    
        @Pointcut("execution(* spring.study.spring.aop.Player.song(..))")
        public void song(){}
        
        @Before("song()")
        public void taskSeats(){
            System.out.println("the audience is taking their seats annotation");
        }
        
        @Before("song()")
        public void turnOffCellPhones(){
            System.out.println("the audience is turning off their cellphones annotation");
        }
        
        @AfterReturning("song()")
        public void applaud(){
            System.out.println("clap clap clap annotation");
        }
        
        @AfterThrowing("song()")
        public void demandRefund(){
            System.out.println("boo! we want our money back! annotation");
        }
    }
    package spring.study.spring.aop;
    
    public class Player {
    
        public void song(){
            System.out.println("la la la la la la");
        }
    }
    package spring.study.spring.aop;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Hello world!
     *
     */
    public class App 
    {
        public static void main( String[] args )
        {
            System.out.println( "Hello World!" );
            ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
            Player p = (Player)ctx.getBean("player");
            p.song();
        }
    }

    输出:

    the audience is taking their seats annotation
    the audience is turning off their cellphones annotation
    la la la la la la
    clap clap clap annotation

    (aop中还有一个叫环绕的,使用方式不管在xml配置还是注解方式都差不多,这里就不做介绍了)

  • 相关阅读:
    8.7题解
    2019.9.16 csp-s模拟测试44 反思总结
    洛谷P3168 [CQOI2015]任务查询系统
    洛谷P2468 [SDOI2010]粟粟的书架
    2019.8.14 NOIP模拟测试21 反思总结
    2019.8.13 NOIP模拟测试19 反思总结
    2019.8.12 NOIP模拟测试18 反思总结
    大约是个告别【草率极了】
    2019.8.10 NOIP模拟测试16 反思总结【基本更新完毕忽视咕咕咕】
    2019.8.9 NOIP模拟测试15 反思总结
  • 原文地址:https://www.cnblogs.com/tiramisuyj/p/4746382.html
Copyright © 2020-2023  润新知