前面了解了典型的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. vbscript 中对excel常见操作

    vbs 对excel的操作 删除.修改单元格,设置字体.背景色dim oExcel,oWb,oSheet Set oExcel= CreateObject("Excel.Applicatio ...

  2. mysqy 特别小点

    1从另外一个 表的查询结果中查数据(没有条件(无where)) SELECT * FROM (SELECT * FROM students) as aSELECT * FROM student     ...

  3. spark第六篇:Spark Streaming Programming Guide

    预览 Spark Streaming是Spark核心API的扩展,支持高扩展,高吞吐量,实时数据流的容错流处理.数据可以从Kafka,Flume或TCP socket等许多来源获取,并且可以使用复杂的 ...

  4. UnityError The same field name is serialized multiple times in the class or its parent class. This is not supported: Base(MonoBehaviour) i

    相同的字段名在类或其父类中被多次序列化.这是不支持的, 这里指的是 变量i . 写如下两个脚本挂到新项目的相机上运行就会出现这个问题: public class Father : MonoBehavi ...

  5. (转)开发监控Linux 内存 Shell 脚本

    原文:http://blog.csdn.net/timchen525/article/details/76474017 题场景: 开发Shell 脚本判断系统剩余内存的大小,如果低于100MB,就邮件 ...

  6. 信号和槽:Qt中最差劲的创造

    不要被这个标题唬住了,实际上我是非常认可Qt的.在C++实现的开源产品中没有哪一个的API风格比得上Qt,拥有高度一致性,符合常识,符合直觉,几乎不用学就可以直接上手.或许是由于我们摆脱不了马太效应的 ...

  7. Tor网络介绍

    Tor网络介绍 1.Tor的全称是“The Onion Router”,“An anonymous Internet communicaton system:通过Tor访问一个地址时,所经过的节点在T ...

  8. IDEA 中,编译后不拷贝 mybatis 配置的 mapper 的 xml 文件

    在maven项目的pom.xml配置文件里添加 <build> <resources> <resource> <directory>src/main/j ...

  9. nyoj 600——花儿朵朵——【离散化、线段树插线问点】

    花儿朵朵 时间限制:1000 ms  |  内存限制:65535 KB 难度:5   描述 春天到了,花儿朵朵盛开,hrdv是一座大花园的主人,在他的花园里种着许多种鲜花,每当这个时候,就会有一大群游 ...

  10. 白话SpringCloud | 第七章:分布式配置中心的使用

    前言 介绍完服务的容错保护处理,接下来我们来了解下关于分布式配置中心的相关知识和使用.众所周知,随着项目的越来越多,日益庞大,每个子项目都会伴随着不同的配置项,于此也就多了很多的配置文件.倘若某些配置 ...