zoukankan      html  css  js  c++  java
  • 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配置还是注解方式都差不多,这里就不做介绍了)

  • 相关阅读:
    SQL SERVER 2005生成带数据的脚本文件 [work around]
    VB.NET窗体关闭事件
    Code::Blocks The open source, cross platform, free C++ IDE.
    VB Twips And Pixels 缇和像素
    JQuery 鼠标点击其它地方隐藏层
    Asp.net 基于Form的权限方法备忘
    JQuery Highcharts图表控件多样式显示多组数据
    ASP.NET中动态获取数据使用Highcharts图表控件
    使用windows服务和.NET FileSystemWatcher对象来监控磁盘文件目录的改变
    【JQuery插件】Select选择框的华丽变身
  • 原文地址:https://www.cnblogs.com/tiramisuyj/p/4746382.html
Copyright © 2011-2022 走看看