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.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">

<!-- (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 {
    @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();
    }
}

输出结果

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对象也被代理了.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值