Spring基于advisor配置aop过程解析

 更新时间:2020年10月26日 14:26:52   作者:Y_wee  
这篇文章主要介绍了Spring基于advisor配置aop过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1、目标类

package com.gec.target;

public class Hadoop { 
  public void eatting() {
    System.out.println("大象正在吃东西 1"); 
    try {
      //耗时5秒 
      Thread.sleep(5000); 
    } catch (InterruptedException e) { 
      e.printStackTrace(); 
    } 
  }
}

2、增强类,此类必须要实现增强方位接口

package com.gec.advice; 

import org.springframework.aop.MethodBeforeAdvice; 
import java.lang.reflect.Method; 

public class BeforeMethodAdvice implements MethodBeforeAdvice { 
  @Override
	public void before(Method method, Object[] objects, Object o) throws Throwable { 
    System.out.println("how are you"); 
  } 
}

3、配置文件

<?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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd                          http://www.springframework.org/schema/util                                     http://www.springframework.org/schema/util/spring-util-4.3.xsd             http://www.springframework.org/schema/context                                    http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop                                    http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<aop:aspectj-autoproxy /> 
  <bean id="beforeMethodAdvice" class="com.gec.advice.BeforeMethodAdvice" /> 
  <bean id="hadoop" class="com.gec.target.Hadoop" /> 
  <aop:config> 
    <!--定义一个切面--> 
    <aop:advisor advice-ref="beforeMethodAdvice" pointcut="execution (* eatting(..))" /> 
  </aop:config> 
</beans>

4、测试

public static void main(String[] args) { 
  ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); 
  Hadoop hadoop= (Hadoop) ctx.getBean("hadoop"); 
  hadoop.eatting(); 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • springboot整合Atomikos的示例详解

    springboot整合Atomikos的示例详解

    这篇文章主要为大家详细介绍了几种分布式事务的解决方案的两阶段提交Atomikos,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下
    2024-11-11
  • Java单链表反转图文教程

    Java单链表反转图文教程

    这篇文章主要给大家介绍了关于Java单链表反转的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 浅析java中print和println的区别

    浅析java中print和println的区别

    以下是对java中print和println的区别进行了详细的分析介绍,需要的朋友可以过来参考下
    2013-08-08
  • Java+OpenCV实现图片中的人脸识别

    Java+OpenCV实现图片中的人脸识别

    这篇文章主要介绍了如何利用java opencv实现人脸识别功能,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • Spring 代码技巧梳理总结让你爱不释手

    Spring 代码技巧梳理总结让你爱不释手

    这篇文章主要分享了Spring 代码技巧梳理总结,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-06-06
  • Spring Cloud Feign实现文件上传下载的示例代码

    Spring Cloud Feign实现文件上传下载的示例代码

    Feign框架对于文件上传消息体格式并没有做原生支持,需要集成模块feign-form来实现,本文就详细的介绍一下如何使用,感兴趣的可以了解一下
    2022-02-02
  • Java8新特性lambda表达式有什么用(用法实例)

    Java8新特性lambda表达式有什么用(用法实例)

    这篇文章主要介绍了Java8新特性lambda表达式有什么用,着重以实例讲解lambda表达式,需要的朋友可以参考下
    2014-06-06
  • Mybatis批量提交实现步骤详解

    Mybatis批量提交实现步骤详解

    这篇文章主要介绍了Mybatis批量提交实现步骤详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-12-12
  • springboot集成nacos报错:get data from Nacos error,dataId:null.yaml的原因及解决方法

    springboot集成nacos报错:get data from Nacos 

    这篇文章给大家介绍了springboot集成nacos报错:get data from Nacos error,dataId:null.yaml的原因及解决方法,如果又遇到相同问题的朋友可以参考阅读本文
    2023-10-10
  • 深入Spring Boot实现对Fat Jar jsp的支持

    深入Spring Boot实现对Fat Jar jsp的支持

    这篇文章主要介绍了深入Spring Boot实现对Fat Jar jsp的支持,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06

最新评论