1. 程式人生 > >Spring-AOP @AspectJ進階之繫結類註解物件

Spring-AOP @AspectJ進階之繫結類註解物件

概述

@within()和@target()函式可以將目標類的註解物件繫結到增強方法中。

我們通過@within()演示註解繫結的操作

例項

這裡寫圖片描述

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

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物件也被代理了.