码迷,mamicode.com
首页 > 编程语言 > 详细

Spring:使用Spring AOP时,如何获取目标方法上的注解

时间:2017-12-04 15:32:47      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:adduser   package   XML   实现类   auto   password   ice   alt   测试   

 

 

当使用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();

 

Spring:使用Spring AOP时,如何获取目标方法上的注解

标签:adduser   package   XML   实现类   auto   password   ice   alt   测试   

原文地址:http://www.cnblogs.com/djoker/p/7977166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!