前面了解了典型的AOP基于配置的使用方法,下面介绍下如何依赖于注解来实现AOP。

基于注解降低了配置文件的复杂程度,但是引入了程序间的耦合,其中的优劣待用户自己判断了。

需要注意的是,确定AspectJ与JDK之间的版本,否则会报错,详情请见

  首先看一下基于注解的切面类,这时的切面不仅仅是一个POJO类了,与AOP进行了紧密的耦合。但是配置过程和方式都与原来的方式差不多。

package com.spring.test.chap44;

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;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Audience {
@Pointcut("execution(* com.spring.test.chap44.Instrumentalist.perform(..))")
public void performance(){} @Before("performance()")
public void takeSeats(){
System.out.println("takeSeats()");
}
@Before("performance()")
public void turnOffCellphones(){
System.out.println("turnOffCellphones()");
}
@AfterReturning("performance()")
public void applaud(){
System.out.println("applaud()");
}
@AfterThrowing("performance()")
public void demandRefund(){
System.out.println("demandRefund()");
}
}

  接下来是其他一些必不可少的类:

  切点接口类:

package com.spring.test.chap44;

public interface Performer {
public void perform();
}

  切点实现类:

package com.spring.test.chap44;

import org.springframework.stereotype.Component;

@Component
public class Instrumentalist implements Performer{
public void perform() {
System.out.println("__________ perform ___________");
}
}

  测试类:

package com.spring.test.chap44;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); Performer performer = (Performer)ctx.getBean("xingoo");
performer.perform();
}
}

  下面是重点的配置文件

  此时的配置文件注意要使spring知道哪一个是普通的bean,哪一个是通知。因此需要加上一个属性,保证AOP自动的识别通知。

<aop:aspectj-autoproxy proxy-target-class="true"/>

  配置文件如下:

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="xingoo" class="com.spring.test.chap44.Instrumentalist"/>
<bean id="audience" class="com.spring.test.chap44.Audience" />
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

  执行结果如下:

turnOffCellphones()
takeSeats()
__________ perform ___________
applaud()

 

  如果需要使用around只需要在切面中添加如下的代码就可以了:

    @Around("performance()")
public void watchPerformance(ProceedingJoinPoint joinpoint){
try{
System.out.println(""); long start = System.currentTimeMillis(); joinpoint.proceed(); long end = System.currentTimeMillis(); System.out.println("time—— "+(end-start)+" millinseconds");
System.out.println("");
}catch(Throwable t){
System.out.println("in watchPerformance Throwable()");
}
}

  对于参数的传递的通知,也与原先通过配置的差不多

  在切面中配置好切点的方法,注意带上参数

    private String str;
@Pointcut("execution(* com.spring.test.chap44.Instrumentalist.perform(String)) && args(str)")
public void performance(String str){} @Before("performance(str)")
public void takeSeats(String str){
System.out.println("takeSeats()"+str);
}

  其他的基本都不用动了,只要把切点的方法,修改成带有参数的就可以了

public class Instrumentalist implements Performer{
public void perform(String str) {
System.out.println("__________ perform ___________" + str);
}
}

【Spring实战】—— 13 AspectJ注解切面的更多相关文章

  1. Spring实战4:面向切面编程

    主要内容 面向切面编程的基本知识 为POJO创建切面 使用@AspectJ注解 为AspectJ的aspects注入依赖关系 在南方没有暖气的冬天,太冷了,非常想念北方有暖气的冬天.为了取暖,很多朋友 ...

  2. Spring学习--用 ASpectJ 注解实现 AOP

    用 AspectJ 注解声明切面: 要在 Spring 中声明 AspectJ 切面 , 只需要在 IOC 容器中将切面声明为 bean 实例.当在 Spring IOC 容器中初始化 AsjectJ ...

  3. spring AOP 编程--AspectJ注解方式 (4)

    1. AOP 简介 AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, ...

  4. spring AOP编程--AspectJ注解方式

    1. AOP 简介 AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, ...

  5. [Spring实战笔记]4面向切面编程的Spring-代理

    代理 代理(Proxy)是一种设计模式,可以在目标对象实现的基础上,扩展目标对象的功能. 代理对象是对目标对象的扩展,并会调用目标对象. 三种代理模式 静态代理 100% 代理对象与目标对象要实现相同 ...

  6. Spring 3.x企业应用开发实战(11)----基于@AspectJ配置切面

    1.@AspectJ的JDK必须是JDK 5.0+ 基于@AspectJ配置切面 @AspectJ采用注解描述切点.增强,两者只是表达式方式不同,效果相同. @AspectJ语法基础-----切点表达 ...

  7. Spring实战之切面编程

    如果要重用通用功能的话,最常见的面向对象技术是继承(inheritance)或委托(delegation).但是,如果在整个应用中都使用相同的基类,继承往往会导致一个脆弱的对象体系:而使用委托可能需要 ...

  8. spring AOP (使用AspectJ的注解方式 的aop实现) (6)

    目录 一.在 Spring 中启用 AspectJ 注解支持 二.AspectJ 支持 5 种类型的通知注解: 2.1.使用之前的 计算器接口和实现类 ArithmeticCalculator.jav ...

  9. Spring使用AspectJ注解和XML配置实现AOP

    本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...

随机推荐

  1. leetcode 88 Merge Sorted Array 归并排序

    归并排序:先将数组一分为二,将左边部分排序(同样将其一分为二),再将右边部分排序,最后逐层归并.(分治策略)(稳定排序). 算法稳定性 -- 假设在数列中存在a[i]=a[j],若在排序之前,a[i] ...

  2. no git binary found in $path(已解决,但是还有疑问)

    跟同行研究个项目代码,他把代码打包发我后,我解压到本地,路径和我本地个人项目路径基本相同, 但是当执行npm install时,就报了 no git binary found in $path ,这个 ...

  3. 119th LeetCode Weekly Contest Largest Perimeter Triangle

    Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, ...

  4. WPF Hidden和Collapsed

    对于这两种设定,其实已经提示的很清楚了 Visibility="Hidden"不显示元素,但是在布局为元素保留空间 Visibility="Collapsed" ...

  5. [转] JavaScript中的Truthy和Falsy介绍

    [From] http://www.jb51.net/article/59285.htm 与大多数编程语言一样,JavaScript中存在boolean类型,以供逻辑判断使用.不过,和很多其它编程语言 ...

  6. Photoshop入门教程(七):蒙版

    学习心得:蒙版在Photoshop中也是很常用的,学会使用蒙版,可以提高图像处理能力,并且可以保护原片不被破坏,建议多使用一些蒙版. 蒙板是灰度的,是将不同灰度色值转化为不同的透明度,并作用到它所在的 ...

  7. 关于箭头函数的this指向问题

    document.onclick = function(){ // 普通函数的this是在运行的时候才临时绑定的,也就是说,函数不运行,你绝对不可能知道this是谁 // 下面这个函数如果是自调用,t ...

  8. ORACLE--12C--多租户体系架构概念

    一,概念 1,何为多租户? 官网链接: 飞机直达>> 多租户这个概念并不是12C的新特性,而是体系架构,多租户架构使得oracle 数据库成为了一个多租户的容器数据库,也就是contain ...

  9. mysql 命令笔记

    添加密码 mysqladmin -uroot -p password 123456 创建用户只能在10.0.0.0网段下访问数据库grant select,create,insert,update o ...

  10. ubuntu安装VMware Tools报错enter the path to the kernel header files

    $ sudo apt-get install build-essential linux-headers-$(uname -r) cd /lib/modules/$(uname -r)/build/i ...