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进阶之绑定类注解对象相关推荐
- 关于 Spring AOP (AspectJ) 你该知晓的一切
[版权申明]未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) http://blog.csdn.net/javazejian/article/details/54629058 出自[zejian ...
- Spring AOP,AspectJ,CGLIB 有点晕
AOP(Aspect Orient Programming),作为面向对象编程的一种补充,广泛应用于处理一些具有横切性质的系统级服务,如事务管理.安全检查.缓存.对象池管理等.AOP 实现的关键就在于 ...
- Spring AOP + AspectJ Annotation Example---reference
In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...
- Spring AOP / AspectJ AOP 的区别?
Spring AOP / AspectJ AOP 的区别? Spring AOP属于运行时增强,而AspectJ是编译时增强. Spring AOP基于代理(Proxying),而AspectJ基于字 ...
- Spring AOP源码(2)—AspectJAwareAdvisorAutoProxyCreator创建代理对象【两万字】
基于最新Spring 5.x,介绍了Spring AOP中的AspectJAwareAdvisorAutoProxyCreator自动代理创建者的工作流程,对于创建代理对象的源码进行了深度分析! ...
- Spring-AOP @AspectJ进阶之绑定代理对象
概述 实例 概述 使用this()或target()可绑定被代理对象实例,在通过类实例名绑定对象时,还依然具有原来连接点匹配的功能,只不过类名是通过增强方法中同名入参的类型间接决定罢了. 这里我们通过 ...
- Spring-AOP @AspectJ进阶之绑定连接点方法入参
文章目录 概述 实例 概述 我们前面的博文在讲解切点函数时说过args().this().target().@args().@within().@target()和@annotation()这7个函数 ...
- Spring AOP AspectJ 代码实例
本文参考来源 http://examples.javacodegeeks.com/enterprise-java/spring/aop/spring-aop-aspectj-example/ http ...
- Spring AOP AspectJ
本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代码. Pointcut:注入Advice的位置,切入点,一般为某 ...
最新文章
- 【 MATLAB】Subspace algorithm Simulation of TOA - Based Positioning
- 我,谷歌AI编舞师,能根据音乐来10种freestyle,想看霹雳还是爵士芭蕾?
- 双向滚动scrollview
- 特征和工具概览(SQL Server 2008)
- 数据结构之表(5)栈的顺序实现
- iOS之基于FreeStreamer的简单音乐播放器(模仿QQ音乐)
- 使用.net core和abp框架实现动态webapi的简单说明
- 微服务之旅的经验分享
- MyBatis源码解析(十二)——binding绑定模块之MapperRegisty
- “System.Exception: System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本” 的解决方案...
- linux 内核logo 居中,linux logo制作及居中显示
- msm8953 usb记录
- 自学白帽黑客的第一年总结
- root 账号不能登录的解决方法
- 【beef工具-01】神器beef的安装与简介
- socket编程到底是什么
- python实现文字识别软件_文字识别(OCR)CRNN(基于pytorch、python3) 实现不定长中文字符识别...
- 计算机内存的维修方法,电脑修理技巧之内存故障解决
- phpstudy搭建upload-labs
- IIS7应用程序池停止后,如何自动启动
热门文章
- python 程序开机自启动,亲测可用
- supercharge快充_华为10V 2A充电器曝光,支持20W电荷泵快充
- matplotlib 知识点整理:ax与figure
- 机器学习笔记:CNN卷积神经网络
- Java实战应用50篇(一)-Java并发编程:volatile关键字解析
- 深度学习核心技术精讲100篇(三十二)-网易实时数仓实战应用
- python实现三叉树_使用python代码实现三叉搜索树高效率”自动输入提示”功能
- “分库分表 ?选型和流程要慎重,否则会失控
- 1号店交易系统架构如何向「高并发高可用」演进
- Apache Hadoop YARN – ResourceManager--转载