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

上一篇介绍了一些概念,这篇我们开始进行编写代码。

1、编写切点:

如图所示的切点表达式表示当Instrument的play()方法执行时会触发通知。方法表达式以*号开始,标识了我们不关心方法返回值的类型。然后,我们指定了全限定类名和方法名。对于参数列表,我们使用(..)标识切点选择任意的play()方法,无论该方法的入参是什么。当我们需要配置切点仅匹配com.springinaction.springidol包,可以使用within()指示器来限制匹配。

除此之外,spring 2.5还引入一个新的bean()指示器,该指示器允许我们在切点表达式中使用Bean的ID来标识Bean。bean()使用Bean Id或者Bean名称作为参数来限制切点只匹配特定的Bean:

例1:execution(* com.springinaction.springidol.Instrument.play()) and bean(eddie)

例2:execution(* com.springinaction.springidol.Instrument.play()) and !bean(eddie)

AOP匹配元素 描述
<aop:advisor> 定义AOP通知器
<aop:after> 定义AOP后置通知(不管被通知的方法是否执行成功)
<aop:after-returning> 定义AOP after-returning通知
<aop:after-throwing> 定义after-throwing通知
<aop:after-around> 定义AOP环绕通知
<aop:aspectj-autoproxy> 启用@AspectJ注解驱动的切面
<aop:aspect> 定义切面
<aop:before> 定义AOP前置通知
<aop:config> 顶层的AOP配置元素,大多数的<aop:*>元素必须包含在<aop:config>元素内
<aop:declare-parents> 为被通知的对象引入额外的接口,并透明地实现
<aop:pointcut> 定义切点

样例:

pom.xml文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>spring</groupId>
  <artifactId>study.spring.aop</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>study.spring.aop</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>3.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib-nodep</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.6.11</version>
    </dependency>
  </dependencies>
</project>

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">

    <bean id = "audience" class="spring.study.spring.aop.Audience" />
    <bean id = "player" class = "spring.study.spring.aop.Player" />
    
    <aop:config>
        <aop:aspect ref = "audience">
            <aop:before pointcut = "execution(* spring.study.spring.aop.Player.song(..))" method = "taskSeats" />
            <aop:before pointcut = "execution(* spring.study.spring.aop.Player.song(..))" method = "turnOffCellPhones"/>
            <aop:after-returning pointcut = "execution(* spring.study.spring.aop.Player.song(..))" method = "applaud" />
            <aop:after-throwing pointcut = "execution(* spring.study.spring.aop.Player.song(..))" method = "demandRefund"/>
        </aop:aspect>
    </aop:config>
</beans>

java类:

package spring.study.spring.aop;

public class Audience {
    
    public void taskSeats(){
        System.out.println("the audience is taking their seats");
    }
    

    public void turnOffCellPhones(){
        System.out.println("the audience is turning off their cellphones");
    }
    
    public void applaud(){
        System.out.println("clap clap clap");
    }
    
    public void demandRefund(){
        System.out.println("boo! we want our money back!");
    }
}
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
the audience is turning off their cellphones
la la la la la la
clap clap clap

 

ps:上述的切点每次都定义,太麻烦了,可以先定义一个切点,然后引用这个切点即可

<aop:config>
        <aop:aspect ref = "audience">
            <aop:pointcut expression="execution(* spring.study.spring.aop.Player.song(..))" id="song"/>
            <aop:before method="taskSeats" pointcut-ref="song"/>
            <aop:before method="turnOffCellPhones" pointcut-ref="song"/>
            <aop:after-returning method="applaud" pointcut-ref="song"/>
            <aop:after-throwing method="demandRefund" pointcut-ref="song"/>
        </aop:aspect>
    </aop:config>

 

posted @ 2015-08-20 21:34  tiramisuyj  阅读(211)  评论(0编辑  收藏  举报