Spring AOP 使用注解定义切面(转载)

原文地址:http://www.jianshu.com/p/6f40dddd71a5

1.定义切面

下面我们就来定义一场舞台剧中观众的切面类Audience

package com.spring.aop.service.aop;

 

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

 

/**

* <dl>

* <dd>Description:观看演出的切面</dd>

* <dd>Company: 黑科技</dd>

* <dd>@date201693下午9:58:09</dd>

* <dd>@authorKong</dd>

* </dl>

*/

@Aspect

public class Audience {

 

 

/**

* 目标方法执行之前调用

*/

@Before("execution(** com.spring.aop.service.perform(..))")

public void silenceCellPhone() {

System.out.println("Silencing cell phones");

}

 

/**

* 目标方法执行之前调用

*/

@Before("execution(** com.spring.aop.service.perform(..))")

public void takeSeats() {

System.out.println("Taking seats");

}

 

/**

* 目标方法执行完后调用

*/

@AfterReturning("execution(** com.spring.aop.service.perform(..))")

public void applause() {

System.out.println("CLAP CLAP CLAP");

}

 

/**

* 目标方法发生异常时调用

*/

@AfterThrowing("execution(** com.spring.aop.service.perform(..))")

public void demandRefund() {

System.out.println("Demanding a refund");

}

 

}

我们可以看到使用了几种注解,其实AspectJ提供了五中注解来定义通知:

注解

通知

@After

通知方法会在目标方法返回或抛出异常后调用

@AfterRetruening

通常方法会在目标方法返回后调用

@AfterThrowing

通知方法会在目标方法抛出异常后调用

@Around

通知方法将目标方法封装起来

@Before

通知方法会在目标方法执行之前执行

  聪明的你可能已经看到,同样的切点我们写了四遍,这是不科学的,强大的Spring怎么会没有处理的方法呢。其实我们可以使用@Pointcut注解声明一个通用的切点,在后面可以随意使用:

package com.spring.aop.service.aop;

 

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

 

/**

* <dl>

* <dd>Description:观看演出的切面</dd>

* <dd>Company: 黑科技</dd>

* <dd>@date201693下午9:58:09</dd>

* <dd>@authorKong</dd>

* </dl>

*/

@Aspect

public class Audience {

 

/**

* 定义一个公共的切点

*/

@Pointcut("execution(** com.spring.aop.service.Perfomance.perform(..))")

public void performance() {

}

 

/**

* 目标方法执行之前调用

*/

@Before("performance()")

public void silenceCellPhone() {

System.out.println("Silencing cell phones");

}

 

/**

* 目标方法执行之前调用

*/

@Before("performance()")

public void takeSeats() {

System.out.println("Taking seats");

}

 

/**

* 目标方法执行完后调用

*/

@AfterReturning("performance()")

public void applause() {

System.out.println("CLAP CLAP CLAP");

}

 

/**

* 目标方法发生异常时调用

*/

@AfterThrowing("performance()")

public void demandRefund() {

System.out.println("Demanding a refund");

}

 

}

  这样定义一个切点后,后面我们的方法想使用这个切点直接调用切点所在的方法就行了。实际上切面也是一个Java类,我们可以将它装配到Spring中的bean中:

/**

* 声明Audience bean

* @return

*/

@Bean

public Audience audience(){

return new Audience();

}

  但是现在Spring还不会将Audience视为一个切面,即便使用了@AspectJ注解,但它并不会被视为一个切面们这些注解不会被解析,也不会创建将其转化为切面的代理。但我们可以使用JavaConfig,然后在JavaConfig类上使用注解@EnableAspectJAutoProxy注解启动自动代理功能:

package com.spring.aop.config;

 

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.EnableAspectJAutoProxy;

 

import com.spring.aop.service.aop.Audience;

 

/**

* <dl>

* <dd>Description:配置类</dd>

* <dd>Company: 黑科技</dd>

* <dd>@date201693下午10:20:11</dd>

* <dd>@authorKong</dd>

* </dl>

*/

 

@Configuration

//启动AspectJ自动代理

@EnableAspectJAutoProxy

@ComponentScan

public class ConcertConfig {

 

/**

* 声明Audience bean

* @return

*/

@Bean

public Audience audience(){

return new Audience();

}

 

}

如果你想使用XML配置也是可以的,我们要使用Spring aop命名空间中的<aop:aspectj-autoproxy>元素:

<?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:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:cache="http://www.springframework.org/schema/cache"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/cache

http://www.springframework.org/schema/cache/spring-cache.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">

 

 

<context:component-scan base-package="com.spring.aop" />

 

<!-- 启动AspectJ自动代理 -->

<aop:aspectj-autoproxy/>

 

<bean class="com.spring.aop.Audience" />

</beans>

  其实不管使用JAvaConfig还是XmlAspectJ都会为使用@ApsectJ注解的Bean创建一个代理,这个代理会环绕着所有该切面所匹配的bean

原文地址:https://www.cnblogs.com/xiaolang8762400/p/7022189.html