Spring基于advisor配置aop過(guò)程解析
更新時(shí)間:2020年10月26日 14:26:52 作者:Y_wee
這篇文章主要介紹了Spring基于advisor配置aop過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1、目標(biāo)類
package com.gec.target; public class Hadoop { public void eatting() { System.out.println("大象正在吃東西 1"); try { //耗時(shí)5秒 Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } }
2、增強(qiáng)類,此類必須要實(shí)現(xiàn)增強(qiáng)方位接口
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> <!--定義一個(gè)切面--> <aop:advisor advice-ref="beforeMethodAdvice" pointcut="execution (* eatting(..))" /> </aop:config> </beans>
4、測(cè)試
public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); Hadoop hadoop= (Hadoop) ctx.getBean("hadoop"); hadoop.eatting(); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Spring AOP訪問(wèn)目標(biāo)方法的參數(shù)操作示例
- Springboot使用@Valid 和AOP做參數(shù)校驗(yàn)及日志輸出問(wèn)題
- 詳解使用Spring AOP和自定義注解進(jìn)行參數(shù)檢查
- SpringBoot @ControllerAdvice 攔截異常并統(tǒng)一處理
- Spring注解@RestControllerAdvice原理解析
- SpringMVC @ControllerAdvice使用場(chǎng)景
- Spring如何基于aop實(shí)現(xiàn)操作日志功能
- 關(guān)于Spring AOP使用時(shí)的一些問(wèn)題匯總
- Spring Aop如何給Advice傳遞參數(shù)
相關(guān)文章
Java?Map.values()方法之如何獲取Map集合中的所有鍵值對(duì)象
這篇文章主要介紹了Java?Map.values()方法之如何獲取Map集合中的所有鍵值對(duì)象問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03springboot中@Value的工作原理說(shuō)明
這篇文章主要介紹了springboot中@Value的工作原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Java構(gòu)建樹(shù)形菜單的實(shí)例代碼(支持多級(jí)菜單)
這篇文章主要介紹了Java構(gòu)建樹(shù)形菜單的實(shí)例代碼(支持多級(jí)菜單),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-09-09