Spring:使用Spring AOP時,如何獲取目標方法上的注解


 

 

當使用spring AOP時,判斷目標方法上的注解進行相關操作,如緩存,認證權限等

自定義注解

package com.agent.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.stereotype.Component;

@Component
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {

    public boolean isEnable() default true;
}

 

Spring AOP的AspectJ

package com.agent.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import com.agent.annotation.MyAnnotation;

@Component
@Aspect
public class LogUtil {

    @Around("@annotation(com.agent.annotation.MyAnnotation)")
    public Object logWrited(ProceedingJoinPoint point) throws Throwable {
        
        
        Object[] args = point.getArgs();
        Class<?>[] argTypes = new Class[point.getArgs().length];
        for (int i = 0; i < args.length; i++) {
              argTypes[i] = args[i].getClass();
        }
        Method method = null;
        try {
            method = point.getTarget().getClass()
                    .getMethod(point.getSignature().getName(), argTypes);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }

        MyAnnotation ma = method.getAnnotation(MyAnnotation.class);
        System.out.println(ma.isEnable());
        
        return point.proceed();
    }
    
    
}

 

Service接口

package com.agent.service;

public interface UserService {

    void addUser(String name, String password);
    
}

 

service接口的實現類,被自定義注解所注解

package com.agent.service.impl;

import org.springframework.stereotype.Service;

import com.agent.annotation.MyAnnotation;
import com.agent.service.UserService;

@Service(value="userService")
public class UserServiceImpl implements UserService {

    @Override
    @MyAnnotation
    public void addUser(String name, String password) {
        System.out.println("UserServiceImpl.addUser()...... name: " + name + "; password: " + password);
    }

}

 

測試類:

package com.agent.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.agent.service.UserService;

public class AOPTest {

    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService us = (UserService)ac.getBean("userService");
        us.addUser("張三", "188");
    }

}

Spring的配置文件:

<?xml version="1.0" encoding="utf-8"?>

<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.agent" />
    
<!--     <bean id="aspect" class="com.agent.aop.LogUtil" />
    <aop:config>
        <aop:aspect ref="aspect">
            <aop:pointcut expression="execution(* add*(..))" id="mypointcut"/>
            <aop:after method="logWrited" pointcut-ref="mypointcut"/>
            <aop:around method="logWrited" pointcut-ref="mypointcut" />
        </aop:aspect>
    </aop:config>
     -->
    <aop:aspectj-autoproxy/>
</beans>

測試結果:

 

 

如果使用的是接口的模式,而注解在實現類上,則不能使用如下方式獲取目標方法的對象,因為該方式獲取的是該類的接口或者頂級父類的方法的對象

        MethodSignature methodSignature = (MethodSignature)point.getSignature();
        Method method = methodSignature.getMethod();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM