spring的aop切面的简单应用

来源:互联网 发布:pack php java 编辑:程序博客网 时间:2024/05/16 08:39

转:http://zywang.iteye.com/blog/974226

Spring3.0中的AOP配置方法

第一种配置方法:使用@AspectJ标签

  1. 在配置文件中添加<aop:aspectj-autoproxy/>注解
  2. 创建一个Java文件,使用@Aspect注解修饰该类
  3. 创建一个方法,使用@Before、@After、@Around等进行修饰,在注解中写上切入点的表达式

说明:上述Java文件创建好后,需要将其在Spring的容器中进行声明,可以在配置文件中定义<bean/>节点,也可以使用@Component组件进行修饰

import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;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.springframework.stereotype.Component;/** * 基于注解的AOP日志示例 * @author ZYWANG 2011-3-24 */@Component@Aspectpublic class AopLog {//方法执行前调用@Before("execution (* com.zywang.services.impl.*.*(..))")public void before() {System.out.println("before");}//方法执行后调用@After("execution (* com.zywang.services.impl.*.*(..))")public void after() {System.out.println("after");}//方法执行的前后调用@Around("execution (* com.zywang.services.impl.*.*(..))")public Object around(ProceedingJoinPoint point) throws Throwable{System.out.println("begin around");Object object = point.proceed();System.out.println("end around");return object;}//方法运行出现异常时调用@AfterThrowing(pointcut = "execution (* com.zywang.services.impl.*.*(..))",throwing = "ex")public void afterThrowing(Exception ex){System.out.println("afterThrowing");System.out.println(ex);}}

上面这段代码中多次使用了重复的切入点,这种情况下,可以使用@Pointcut标注,来修改一个切入点方法(这个方法不需要参数和方法体),然后就可以在@Before等标注中引用该方法作为切入点,示例如下:

import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;/** * 基于注解的AOP日志示例 * @author ZYWANG 2011-3-24 */@Component@Aspectpublic class AopLog {@Pointcut("execution (* com.iflysse.school.services.impl.*.*(..))")public void pointcut(){}//方法执行前调用@Before("pointcut()")public void before() {System.out.println("before");}//方法执行的前后调用@Around("pointcut()")public Object around(ProceedingJoinPoint point) throws Throwable{System.out.println("begin around");Object object = point.proceed();System.out.println("end around");return object;}}

第二种配置方法:基于配置文件的配置

  1. 创建一个Java文件,并指定一个用于执行拦截的方法,该方法可以有0个或多个参数
  2. 在Spring配置文件中注册该Java类为一个Bean
  3. 使用<aop:config/>、<aop:aspect/>等标签进行配置
import org.aspectj.lang.ProceedingJoinPoint;/** * 基于配置文件的AOP日志示例 * @author ZYWANG 2011-3-24 */public class AopLog {//方法执行的前后调用public Object runOnAround(ProceedingJoinPoint point) throws Throwable{System.out.println("begin around");Object object = point.proceed();System.out.println("end around");return object;}}

Spring配置文件

<bean id="aopLog" class="com.iflysse.school.aop.AopLog"></bean><aop:config><aop:aspect ref="aopLog"><aop:around method="runOnAround" pointcut="execution (* com.zywang.services.impl.*.*(..))"/></aop:aspect></aop:config>

注意:上面这个示例使用的是around方式的拦截,该方法要求Java类中的方法有一个ProceedingJoinPoint类型的参数

使用第二种方式的AOP配置,在Eclipse(有SpringIDE插件)中被拦截到的方法中有标识显示

 

以上配置基于Spring 3.0.5 进行设置,参考其《Reference Documentation》


转:http://blog.csdn.net/rongyongfeikai2/article/details/6755774

利用SPring AOP配置切面的一个例子

这个例子,就是对于DukePerformer类,在它的演奏方法perform执行前,输出观众找座位takeSeat和关手机turnOffPhone,在执行后,输出观众鼓掌applaud。

将代码提取出来,独立于一个模块中,就是切面编程。主要还是为了松散耦合。

首先定义DukePerformer类:

package com.XinXiangShop.AOP;public class DukePerformer implements Performer{private String name;public void setName(String name){this.name=name;}public String getName(){return this.name;}@Overridepublic void perform() {// TODO Auto-generated method stubSystem.out.println(this.name+" sing a song.");}}

然后定义观众类:

package com.XinXiangShop.AOP;public class Audience {public void takeSeat(){System.out.println("The audiences take seat.");}public void turnOffPhone(){System.out.println("The audiences turn off the phone.");}public void applaud(){System.out.println("CLAP CLAP CLAP...");}public void unHappy(){System.out.println("The audiences are unhappy.");}}

对于applilcationContext.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-2.0.xsd">  <!-- AOP学习时的配置 -->  <bean id="DukePerformer" class="com.XinXiangShop.AOP.DukePerformer">  <property name="name" value="duke"/>  </bean>  <bean id="audience" class="com.XinXiangShop.AOP.Audience"/>  <aop:config>  <aop:aspect ref="audience">  <aop:before method="takeSeat" pointcut="execution(* *.perform(..))"/>  <aop:before method="turnOffPhone" pointcut="execution(* *.perform(..))"/>  <aop:after-returning method="applaud" pointcut="execution(* *.perform(..))"/>  <aop:after-throwing method="unHappy" pointcut="execution(* *.perform(..))"/>  </aop:aspect>  </aop:config>  <!-- AOP学习时的配置 --></beans>

测试的代码如下:

package com.XinXiangShop.AOP;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;public class Main {public static void main(String[] args){ApplicationContext ctx=new FileSystemXmlApplicationContext("src/com/XinXiangShop/AOP/applicationContext.xml");Performer per=(Performer)ctx.getBean("DukePerformer");per.perform();}}

如果在执行时报java.lang.NoClassDefFoundError: org/aspectj/weaver/BCException那么是缺少 aspectjweaver-1.5.3.jar

程序运行的结果为:

The audiences take seat.
The audiences turn off the phone.
duke sing a song.
CLAP CLAP CLAP...


0 0
原创粉丝点击