SpringAop的简单实现

AOP当中的概念:

  • 1、切入点(Pointcut):在哪些类,哪些方法上切入(where);
  • 2、增强(Advice): 早期翻译为通知,在方法执行的什么时机(when:方法前/方法后/方法前后)做什么(what:增强的功能);
  • 3、切面(Aspect): 切面=切入点+增强,通俗点就是:在什么时机,什么地点,做什么增强!
  • 4、织入(Weaving): 把切面加入到对象,并创建出代理对象的过程。(该过程由Spring来完成)。

开启AOP

SpringMvc 开启方式

  • 1、引入依赖

    <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency>
  • 2、在spring配置文件中加入

    <!--AOP注解解析器-->

    aop:aspectj-autoproxy/

SpringBoot 开启方式

  • 1、引入依赖

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
  • 2、在application.properties中加入配置
    spring.aop.auto=true

SpringBoot 简单实践

1、切面

1@Component//让spring管理该bean 2@Aspect//切面 3public class AOPTest { 4 /* 5 *切点 6 */ 7 @Pointcut("execution(* com.kismet.p2p.mgrsite.controller.AOPController.test(..))") 8 public void aop(){} 9 10 @Around("aop()")//增强以around增强为例 11 public Object around(ProceedingJoinPoint point){ 12 Object result = null; 13 try { 14 System.out.println("AOP开始了"); 15 /* 16 * 执行被切点中的方法 17 * result接受到的方法放回值,该例子中即为"lalaalal" 18 */ 19 result = point.proceed(); 20 System.out.println("AOP结束了"); 21 } catch (Throwable throwable) { 22 throwable.printStackTrace(); 23 } 24 return result; 25 } 26}

说明:

  • 1、execution(<修饰符>? <返回类型> <声明类型>? <方法名>(<参数>) <异常>?) ?表示可选参数
  • 2、支持*匹配规则
  • 3、其中带问号的为选填.例如上述切点表达式的意思为:
    (* com.kismet.p2p.mgrsite.controller.AOPController.test(..)) 方法com.kismet.p2p.mgrsite.controller.AOPController.test方法的所有返回类型,所有参数类型,作为切点.

2、被切点

1package com.kismet.p2p.mgrsite.controller; 2@Controller 3public class AOPController { 4 @RequestMapping("test") 5 public String test(){//该方法的引用即为上述切面中的切点 6 System.out.println("被测试"); 7 return "lalaalal"; 8 } 9}

3、运行方法test结果

AOP开始了
被测试
lalaalal
AOP结束了

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

javaWeb

<fontsize5style"lineheight:32px;letterspacing:1px;"<fontcolorred一</font:拦截器:是在面向切面编程的就是在你的service或者一个方法,前调用一个方法,或者在方法后调用一个方法比如动态代理就是拦截器的简单实现,springmvc的aop中的前置通知和后置通知。

Spring AOP @Aspect 基本用法

Spring使用的AOP注解分为三个层次:前提条件是在xml中放开了<aop:aspectjautoproxyproxytargetclass"true"/<!开启切面编程功能1、@Aspect放在类头上,把这个类作为一个切面。2、@Pointcut放在方法头上,定义一个可被别的方法引用的切入点

SpringAOP

Aspect切面:一个关注点的模块化,这个关注点可能会横切多个对象Joinpoint连接点:程序执行过程中的某个特定的点Advice通知:在切面的某个连接点上执行的动作Pointcut切入点:匹配连接点的断言,在AOP的通知和一个切入点表达式关联Introduction引入:在不修改类代码的前提下,为类添加新的方法