Aspect Oriented Programming 面向切面编程

在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业。

需要在bean.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <context:annotation-config />
    <context:component-scan base-package="com.bjsxt"/>

     <aop:aspectj-autoproxy />

</beans>

使用aspectJ需要导入aspectj的jar包 aspectjrt(runtime)和aspectjweave(编织)

如果被代理的类没有实现接口还要导入cglib-nodep的jar包,它可以修改字节码来实现代理。

在代码中的写法:

package com.bjsxt.aop;

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;

@Aspect
@Component//这个注解不能少,这样spring才会将其识别为组件
public class LogInterceptor {
    @Pointcut("execution(public * com.bjsxt.service..*.add(..))")
    public void myMethod(){};//这个method相当于上面定义Pointcut的name,写法比较特殊,但是aspectj就这么搞的。
   
    @Before("myMethod()")//这里的myMethod就相当于Pointcut所指的内容了("execution(public * com.bjsxt.service..*.add(..))")。
    public void before() {
        System.out.println("method before");
    }
   
    @Around("myMethod()")
    public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("method around start");
        pjp.proceed();//表示放行,可以执行其他拦截器,没有就执行方法本身
        System.out.println("method around end");
    }
   
}

附几种Pointcut写法:

package com.xyz.someapp;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;@Aspect
public class SystemArchitecture {/*** A join point is in the web layer if the method is defined* in a type in the com.xyz.someapp.web package or any sub-package* under that.*/@Pointcut("within(com.xyz.someapp.web..*)")public void inWebLayer() {}/*** A join point is in the service layer if the method is defined* in a type in the com.xyz.someapp.service package or any sub-package* under that.*/@Pointcut("within(com.xyz.someapp.service..*)")public void inServiceLayer() {}/*** A join point is in the data access layer if the method is defined* in a type in the com.xyz.someapp.dao package or any sub-package* under that.*/@Pointcut("within(com.xyz.someapp.dao..*)")public void inDataAccessLayer() {}/*** A business service is the execution of any method defined on a service* interface. This definition assumes that interfaces are placed in the* "service" package, and that implementation types are in sub-packages.** If you group service interfaces by functional area (for example,* in packages com.xyz.someapp.abc.service and com.xyz.def.service) then* the pointcut expression "execution(* com.xyz.someapp..service.*.*(..))"* could be used instead.** Alternatively, you can write the expression using the 'bean'* PCD, like so "bean(*Service)". (This assumes that you have* named your Spring service beans in a consistent fashion.)*/@Pointcut("execution(* com.xyz.someapp.service.*.*(..))")public void businessService() {}/*** A data access operation is the execution of any method defined on a* dao interface. This definition assumes that interfaces are placed in the* "dao" package, and that implementation types are in sub-packages.*/@Pointcut("execution(* com.xyz.someapp.dao.*.*(..))")public void dataAccessOperation() {}}

Spring AOP 面向切面编程相关注解相关推荐

  1. Java绝地求生—Spring AOP面向切面编程

    Java绝地求生-Spring AOP面向切面编程 背景 动态代理 构建被代理对象 自动生成代理 调用动态代理 Spring方法 方式一:使用Spring的API接口 方式二:使用自定义类 方式三:使 ...

  2. Spring AOP面向切面编程

    AOP面向切面编程: AOP(Aspect Oriented Programming),即面向切面编程,它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公 ...

  3. Spring AOP(面向切面编程)

    AOP(Aspect Oriented Programming),也就是面向切面编程,作为面向对象编程的一种补充,AOP已经成为一种比较成熟的编程方式.可以这样理解:OOP是从静态角度考虑程序结构,而 ...

  4. JavaEE——Spring AOP(面向切面编程)

    目录 1.面向切面编程(AOP) 2.AOP术语 3.AOP类型 4.AOP 的优势 5.Spring AOP 的代理机制 6.Spring AOP 连接点 7.Spring AOP 通知类型 8.基 ...

  5. Spring Aop面向切面编程自动注入

    1.面向切面编程 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面的过程叫做面向切面编程 2.常用概念 原有功能:切点,pointcut 前置通知:在切点之前执行的功能,befor ...

  6. Spring AOP 面向切面编程

    AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件 ...

  7. java框架013——Spring AOP面向切面编程

    一.Spring AOP简介 AOP的全称是Aspect-Oriented Programming,即面向切面编程(也称面向方面编程).它是面向对象编程(OOP)的一种补充,目前已成为一种比较成熟的编 ...

  8. Spring AOP面向切面编程:理解篇(一看就明白)

    一直想着怎么去通俗的讲解AOP,看了一篇文章受到了启发(https://blog.csdn.net/qukaiwei/article/details/50367761),下面我加入自己的理解,咱们来说 ...

  9. Spring aop面向切面编程概述

    aop概述 1.AOP为Aspect Oriented Programming的缩写,意为:面向切面编程.将程序中公用代码进行抽离,通过动态代理实现程序功能的统一维护的一种技术.使代码耦合性降低,提高 ...

最新文章

  1. python【数据结构与算法】B树概念解析和实现
  2. 为什么说嵌入式开发比单片机要难很多?工程师谈谈自己的感悟
  3. 肯耐珂萨助力世界500强零售企业在线春招:单次面试3000人
  4. 投票选举 算法_区块链主流共识算法一文全通
  5. oracle用户密码复杂度查询,11gR2 Database用户密码复杂度验证
  6. Tuxera NTFS使用教程:关于Tuxera NTFS mac还有你不知道的用法
  7. vs2015professional过期后登录微软账户仍然不能使用的解决方法
  8. Python数据结构与算法(2.1)——线性表的基本概念
  9. 学习笔记之正则表达式
  10. ubuntu 18.04使用aqt安装QT5.12
  11. linux清理dns缓存命令,Ubuntu下清空DNS缓存 提升访问速度
  12. 局域网与城域网(1)(网工)
  13. SQL AUTO INCREMENT
  14. matlab机器人工具箱 欧拉角,Matlab机器人工具箱(一)
  15. 组长偷偷通知裁员名单有我,但HR却迟迟不找,现在没人布置工作,天天闲着好尴尬!...
  16. ROS path问题解决方案
  17. HBase原理 | HBase分区影响与合理分区设置
  18. springCloud——Dalston.SR5升级到Greenwich.SR2
  19. 如何有效控制项目范围?
  20. 微星B450M安装ubuntu 18.04 BIOS更改启动顺序

热门文章

  1. 2019.2.7 区块链论文翻译
  2. Kafka消费者重置offset读取数据
  3. Ubuntu中设置防火墙的开启与关闭
  4. datatable1.9 与datatable1.10以数据差异
  5. Zabbix监控网络设备日志文件及字段报警
  6. 获取各种常见形状的位图
  7. Hadoop “Unable to load native-hadoop library for y
  8. 如安装flashplayer旧版本
  9. CentOS6.2 KVM 虚拟机命令行安装配置
  10. BSTR 、LPCTSTR、CString附C语言串基本操作