当使用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时,如何获取目标方法上的注解的更多相关文章

  1. AOP中ProceedingJoinPoint获取目标方法,参数,注解

    private void saveLog(ProceedingJoinPoint jp,long time)throws Throwable { package com.cy.pj.common.as ...

  2. 学习spring第6天(aop获取目标方法参数)

    关于<aop:around>中的方法,需要第一个参数为ProceedJoinPoint,在方法体中通过该参数调用proceed()才能使目标方法得到调用. 当一个切面中有多个<aop ...

  3. spring aop获取目标对象的方法对象(包括方法上的注解)

    这两天在学习权限控制模块.以前看过传智播客黎活明老师的巴巴运动网视频教程,里面就讲到权限控制的解决方案,当时也只是看看视频,没有动手实践,虽说看过几遍,可是对于系统中的权限控制还是很迷茫,所以借着这次 ...

  4. thinkphp5 查询的数据是对象时,获取原始数据方法

    获取原始数据 如果你定义了获取器的情况下,希望获取数据表中的原始数据,可以使用: $user = User::get(1); // 通过获取器获取字段 echo $user->status; / ...

  5. SpringMVC + Spring + MyBatis 学习笔记:在类和方法上都使用RequestMapping如何访问

    系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 先看代码: @Requ ...

  6. 黑马-Spring(IOC&DI) AOP

    IOC(控制翻转) 概念 把对象的创建.初始化.销毁等工作交给spring容器来做 案例 环境 步骤 1.  写一个HelloWorld类 2.  写一个配置文件   把hello类放到spring容 ...

  7. spring5 源码深度解析----- 创建AOP代理之获取增强器

    在上一篇的博文中我们讲解了通过自定义配置完成了对AnnotationAwareAspectJAutoProxyCreator类型的自动注册,那么这个类到底做了什么工作来完成AOP的操作呢?首先我们看看 ...

  8. Spring学习之Aop的各种增强方法

    AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5一样的注解,但并没有使用AspectJ的 ...

  9. Spring中的AOP 专题

    Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advi ...

随机推荐

  1. Django2.0——请求与响应(上)

    客户端与服务段通过http协议进行数据的传输,而http协议是一种双向单工的,且主动发起连接的只有客户端.故数据的传送就离不开请求和响应,客户端每发起一个请求,服务端就是返回一个响应.在django的 ...

  2. ubuntu搭建web服务器

    https://www.linuxidc.com/Linux/2015-11/125477.htm 到“sudo apt-get install libapache2-mod-php5”出现1错误.

  3. UVA 125 统计路径条数 FLOYD

    这道题目折腾了我一个下午,本来我的初步打算是用SPFA(),进行搜索,枚举出发点,看看能到达某个点多少次,就是出发点到该点的路径数,如果出现环,则置为-1,关键在于这个判环过程,如果简单只找到某个点是 ...

  4. TiKV 在京东云对象存储元数据管理的实践

    京东云对象存储是在 2016 年作为公有云对外公开的,主要特点是可靠.安全.海量.低成本,应用于包括一些常用的业务场景,比如京东内部的京东商城视频/图片云存储,面向京东云公有云外部的开发者的服务,和面 ...

  5. LeetCode Input Initial Code

    说明 LeetCode提供的样本输入,显示上是数组Array,而后台的实际测试用例则是树TreeNode,链表ListNode等. 如果你是在页面手撸代码直接提交的,那没什么影响. 如果你是在本地ID ...

  6. Druid数据库连接池获取连接阻塞(转载)

    一. 背景        17年公司有个项目组在南京做项目的时候,开发框架用的是spring boot ,数据库连接池用的是druid,但老是遇到socket read timeout的错误,不得已放 ...

  7. Python语言学习:homework2

    三级菜单 # Author:Crystal data = { '北京':{ "昌平":{ "沙河":["oldbay","test ...

  8. android 设置无标题栏主题

    <application android:theme="@style/Theme.AppCompat.Light.NoActionBar">

  9. 阿里云Linux格式化数据盘,分区并挂载一个文件系统

    阿里云一块全新的数据盘挂载到ECS实例后,您必须创建并挂载至少一个文件系统.本示例使用I/O优化实例,操作系统为CentOS 7.6,为一块新的300GiB数据盘(设备名为/dev/vdb)创建一个M ...

  10. 数据库T-SQL语言操作(T-SQL语句、数据库、表、视图、索引)

    T-SQL语言 按用途分四部分 数据定义语言(CREATE,DROP,ALTER) 数据操作语言(INSERT,DELETE,UPDATE) 数据查询语言(SELECT) 数据控制语言(GRANT,R ...