aspectj annotation- used in spring

JAVA软件工程师--注释方式切面编程(Spring AOP技术)

(2010-07-26 23:35:15)

转载

标签:

spring

aop

面向方面编程

web开发

java

注解

annotation

开源框架

it

分类:JAVA高级软件工程师

导论:Aspect Oriented Programming(AOP)即面向切面编程。AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果。它使得代码更加灵活,并且提供了代码的重用性。
                                                                                                                          作者: 徐亮
可以写普通JAVA+配置文件的方式进行切面编程,也可以用注释(annotation)方式进行切面编程。本例中采用后者。
流程:
1、写特殊的JAVA类,它通过添加注释(annotation)方式,指定切面、切入点和通知----->
2、在配置文件中加入一行,以便打开aspectJ注解,使其自动生成代理------>
3、写目标类的目标方法----->
4、测试类中调用目标类的目标方法。

1、写特殊的JAVA类,它通过添加注释(annotation)方式

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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.Service;
@Aspect
//指定这是一个其切面
public class myAnnotationAspect {

//指定切入点
    @Pointcut("execution(* myTarena.*.*(..))")
    public void myPointCut(){}

  //指定通知以及通知的范围
    @Before("myPointCut()")
    public void beforeMethod(){
        System.out.println("前置通知。。。");
    }
   
    @Around("myPointCut()")
    public Object aroundMethod(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("环绕通知上。。。");
        Object obj=pjp.proceed();
        System.out.println("环绕通知下。。。");
        return obj;
    }
   
    @After("myPointCut()")
    public void afterMethod(){
        System.out.println("后置通知。。。");
      
    }   
}

2、在配置文件中加入一行,以便打开aspectJ注解,使其自动生成代理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
           <bean id="myService" class="myTarena.MyService"></bean>
           <bean id="myAnnotationAspect" class="myTarena.myAnnotationAspect"></bean>
          <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

3、写目标类的目标方法
public class MyService implements IMyService {
    public void regist(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("----执行注册操作----");
    }
}

4、测试类中调用目标类的目标方法。
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AspectTest {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("aop-aspect.xml");
        IMyService myService=(IMyService) ctx.getBean("myService");
        myService.regist();

    }
}

@Aspect

public class AuditPointcut {

      @Pointcut(value="@annotation(audit)", argNames="audit")

    public void auditMethod(Audit audit) {}

}

表示加了注释audit的方法就会执行下面的doAudit方法

public class AuditAdvice implements springframework.core.Ordered{

@Around(value = "AuditPointcut.auditMethod(audit)", argNames = "pjp, audit")

      public Object doAudit(ProceedingJoinPoint pjp, Audit audit) throws Throwable {

     

}

}

 

定义注释

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface Audit {

    String operation() default "";

    String component() default "";

    String comments() default "";

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值