码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-08-20 23:48:04      阅读:330      评论:0      收藏:0      [点我收藏+]

标签:

前面介绍了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配置还是注解方式都差不多,这里就不做介绍了)

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

标签:

原文地址:http://www.cnblogs.com/tiramisuyj/p/4746382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!