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 "";

}

aspectj annotation- used in spring相关推荐

  1. Spring AOP + AspectJ Annotation Example---reference

    In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...

  2. 基于 Annotation 拦截的 Spring AOP 权限验证方法

    余 清, 软件工程师, IBM 简介: 使用 Annotation 可以非常方便的根据用户的不同角色,分配访问 Java 方法的权限.在 Java Web 开发中,使用这种方法,可以提高系统的松耦合度 ...

  3. Java Spring源代码学习之How is class annotation evaluated by Spring framework

    Created by Wang, Jerry, last modified on Aug 12, 2016

  4. Spring AOP技术(基于AspectJ)的Annotation开发

    Spring AOP技术(基于AspectJ)的Annotation开发 @(Spring)[aop, spring, xml, Spring, annotation, aspectJ] Spring ...

  5. 在Intellij上面导入项目 AOP示例项目 AspectJ学习 Spring AoP学习

    为了学习这篇文章里面下载的代码:http://www.cnblogs.com/charlesblc/p/6083687.html 需要用Intellij导入一个已有工程.源文件原始内容也可见:link ...

  6. 基于@AspectJ配置Spring AOP之一--转

    原文地址:http://tech.it168.com/j/2007-08-30/200708302209432.shtml 概述 在低版本Spring中定义一个切面是比较麻烦的,需要实现特定的接口,并 ...

  7. Spring中的AOP(三)——基于Annotation的配置方式(一)

    为什么80%的码农都做不了架构师?>>>    AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是 ...

  8. maven aspectj_使用Spring AspectJ和Maven进行面向方面的编程

    maven aspectj Spring框架附带AOP支持. 实际上,如Spring参考文档中所述 , " Spring的关键组件之一是AOP框架. 尽管Spring IoC容器不依赖于AO ...

  9. 使用Spring AspectJ和Maven进行面向方面的编程

    Spring框架附带AOP支持. 实际上,如Spring参考文档中所述 , " Spring的关键组件之一是AOP框架. 尽管Spring IoC容器不依赖于AOP,这意味着您不需要使用AO ...

  10. Spring使用AspectJ开发AOP

    AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...

最新文章

  1. 用thttpd做Web Server
  2. Leetcode 面试题 10.01. 合并排序的数组 (每日一题 20210616)
  3. Java之JVM调优案例分析与实战(1) - 高性能硬件上的程序部署策略
  4. 怎么用汇编语言转换c语言程序设计,C语言是如何转换成汇编语言的6个步骤带你解析...
  5. (一)获取上下文绘图环境
  6. 微服务升级_SpringCloud Alibaba工作笔记0026---Nacos之Linux安装nacos
  7. php时间格式转换成时间戳,php把时间格式转换为时间戳的案例
  8. 非线性光纤光学——光孤子4
  9. 教育行业是永恒不过时的常青藤行业!
  10. 120 行代码实现纯 Web 剪辑视频
  11. 三相直流无刷电机驱动
  12. 赫兹的单位换算_hz是什么单位(频率和赫兹的换算)
  13. python抠图教程_简单几行Python代码实现8秒抠图的AI神器,根本无需PS(附教程)...
  14. JAVA-----乱码的处理 乱码的解决方法总结
  15. 更改电脑软件默认安装位置
  16. Android Studio项目中常见的需要自行创建的资源文件夹的位置(assets、raw、menu、anim等)
  17. 2020/8/3/ATC工具以及AIPP
  18. 无线路由器中继设置(增加无线网络信号)
  19. webpack5-打包css等样式资源,css等样式文件提取,css等样式文件压缩
  20. 频谱仪使用方法图解_【康复技术】肌内效贴布使用方法,肌内效贴布贴法图解...

热门文章

  1. LeetCode 879. 盈利计划
  2. 人脸识别算法不可置疑?真相需要多重验证!
  3. G6 图可视化引擎——核心概念——节点/边/Combo——内置节点——内置节点总览
  4. 征战蓝桥 —— 2014年第五届 —— C/C++A组第5题——锦标赛
  5. 信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言——1101:不定方程求解
  6. Vue.js的基本使用 学习笔记
  7. 【机器视觉】 endwhile算子
  8. 【MFC】定制浮动工具栏
  9. 【MFC】Windows窗口样式
  10. 【Linux】一步一步学Linux——chkconfig命令(148)