spring學習筆記——面向切面編程AOP二

上一篇介紹了一些概念,這篇咱們開始進行編寫代碼。java

一、編寫切點:node

如圖所示的切點表達式表示當Instrument的play()方法執行時會觸發通知。方法表達式以*號開始,標識了咱們不關心方法返回值的類型。而後,咱們指定了全限定類名和方法名。對於參數列表,咱們使用(..)標識切點選擇任意的play()方法,不管該方法的入參是什麼。當咱們須要配置切點僅匹配com.springinaction.springidol包,能夠使用within()指示器來限制匹配。spring

除此以外,spring 2.5還引入一個新的bean()指示器,該指示器容許咱們在切點表達式中使用Bean的ID來標識Bean。bean()使用Bean Id或者Bean名稱做爲參數來限制切點只匹配特定的Bean:express

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

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

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> 定義切點

樣例:maven

pom.xml文件:ui

<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文件:url

<?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類:spa

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>
相關文章
相關標籤/搜索