首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Spring-AOP @AspectJ进阶之绑定类注解对象

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

作者头像
小小工匠
发布2021-08-17 10:04:01
发布2021-08-17 10:04:01
23800
代码可运行
举报
文章被收录于专栏:小工匠聊架构小工匠聊架构
运行总次数:0
代码可运行

概述

@within()和@target()函数可以将目标类的注解对象绑定到增强方法中。

我们通过@within()演示注解绑定的操作


实例

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



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

代码语言:javascript
代码运行次数:0
运行
复制
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;
}

业务类

代码语言:javascript
代码运行次数:0
运行
复制
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");
    }
}

切面

代码语言:javascript
代码运行次数:0
运行
复制
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入参可以引用到连接点处的注解对象。


配置文件

代码语言:javascript
代码运行次数:0
运行
复制
<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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">


<context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.bindTypeAnnoObj"/>


<aop:aspectj-autoproxy proxy-target-class="true"/>


<bean class="com.xgj.aop.spring.advisor.aspectJAdvance.bindTypeAnnoObj.BindTypeAnnoObjectAspect"/>

beans>

测试类

代码语言:javascript
代码运行次数:0
运行
复制
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 {
    @Test
    public 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();
    }
}

输出结果

代码语言:javascript
代码运行次数:0
运行
复制
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对象也被代理了.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/09/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 实例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档