日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring-AOP @AspectJ进阶之绑定类注解对象

發布時間:2025/3/21 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring-AOP @AspectJ进阶之绑定类注解对象 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  • 概述
  • 實例

概述

@within()和@target()函數可以將目標類的注解對象綁定到增強方法中。

我們通過@within()演示注解綁定的操作


實例

代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster



注解(使用的是自定義注解,也可以使用框架提供的注解)

package com.xgj.aop.spring.advisor.aspectJAdvance.bindTypeAnnoObj;import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;//聲明注解的保留期限 @Retention(RetentionPolicy.RUNTIME) // 聲明可以使用該注解的目標類型 @Target(ElementType.TYPE) // 可以被javadoc此類的工具文檔化 @Documented public @interface Monitor { // 定義注解// 聲明注解成員boolean value() default false; }

業務類

package com.xgj.aop.spring.advisor.aspectJAdvance.bindTypeAnnoObj;import org.springframework.stereotype.Component;/*** * * @ClassName: Bussiness* * @Description: bean使用@Component注解,* * 同時標注了@@Monitor注解,所有Bussiness Bean匹配切點, 其@Monitor注解對象將綁定到增強方法中* * @author: Mr.Yang* * @date: 2017年9月12日 下午4:32:23*/@Component @Monitor public class Bussiness {public void dealBussinessOne() {System.out.println("dealBussinessOne executed");}public void dealBussinessTwo() {System.out.println("dealBussinessTwo executed");} }

切面

package com.xgj.aop.spring.advisor.aspectJAdvance.bindTypeAnnoObj;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;/*** * * @ClassName: BindTypeAnnoObjectAspect* * @Description: @Aspect標注的切面* * (1)通過(2)處查找出m對應Monitor類型的注解, 因而真實的切點表達式為@within* (Monitor),當增強方法織入目標 連接點時,增強方法通過m入參可以引用到連接點處的注解對象。* * @author: Mr.Yang* * @date: 2017年9月12日 下午4:27:55*/@Aspect public class BindTypeAnnoObjectAspect {// (1)@Before("@within(m)")public void bindTypeAnno(Monitor m) { // (2)System.out.println("----bindTypeAnnoObject()----");System.out.println(m.getClass().getName());System.out.println("----bindTypeAnnoObject()----");} }

(1)通過(2)處查找出m對應Monitor類型的注解, 因而真實的切點表達式為@within(Monitor),當增強方法織入目標 連接點時,增強方法通過m入參可以引用到連接點處的注解對象。


配置文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- (1)聲明Context命名空間以及Schema文件 (2)掃描類包以及應用注解定義的bean --> <context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.bindTypeAnnoObj"/><!-- 基于@AspectJ切面的驅動器 --> <aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面類 --> <bean class="com.xgj.aop.spring.advisor.aspectJAdvance.bindTypeAnnoObj.BindTypeAnnoObjectAspect"/></beans>

測試類

package com.xgj.aop.spring.advisor.aspectJAdvance.bindTypeAnnoObj;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class BindTypeAnnoObjectAspectTest {@Testpublic void test() {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/aop/spring/advisor/aspectJAdvance/bindTypeAnnoObj/conf-bindTypeAnnoObj.xml");Bussiness bussiness = ctx.getBean("bussiness", Bussiness.class);bussiness.dealBussinessOne();bussiness.dealBussinessTwo();} }

輸出結果

2017-09-12 16:58:15,464 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@292898f5: startup date [Tue Sep 12 16:58:15 BOT 2017]; root of context hierarchy 2017-09-12 16:58:15,684 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJAdvance/bindTypeAnnoObj/conf-bindTypeAnnoObj.xml] ----bindTypeAnnoObject()---- com.sun.proxy.$Proxy6 ----bindTypeAnnoObject()---- dealBussinessOne executed ----bindTypeAnnoObject()---- com.sun.proxy.$Proxy6 ----bindTypeAnnoObject()---- dealBussinessTwo executed

從輸出信息中,com.sun.proxy.$Proxy6,即使用CGLib代理NaiveWaiter時,其類的注解Monitorable對象也被代理了.

總結

以上是生活随笔為你收集整理的Spring-AOP @AspectJ进阶之绑定类注解对象的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。