AOP注解式拦截
1. 自己定义的拦截注解 package com.spring.aop; 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.ANNOTATION_TYPE,ElementType.METHOD,ElementType.TYPE})
@Documented
public @interface MyAction { String value();
}
2. 定义aop切点 package com.spring.aop; import java.lang.reflect.Method; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component; @Aspect
@Component
public class MyAop { //切点切入自己配置的那个注解MyAction
@Pointcut("@annotation(com.spring.aop.MyAction)") //注意这里的写法@annotation() 切入注解
public void annotationPointCat(){} /*
@Pointcut("execution(* com.savage.aop.MessageSender.*(..))") //注意这里的写法execution() 切入类
private void logSender(){}
*/
@Before("annotationPointCat()")//位置为这个包下的所有类、所有方法、不论参数是什么类型
public void before(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
MyAction myAction = method.getAnnotation(MyAction.class);
System.out.println("before: "+myAction.value());
}
@After("annotationPointCat()")
public void after(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
MyAction myAction = method.getAnnotation(MyAction.class);
System.out.println("after: "+myAction.value());
}
}
3. 编写服务类,应用切面 package com.spring.aop; import org.springframework.stereotype.Component; @Component
public class MyServer {
/*
显而易见,利用注解式拦截,会提高切面的复用率
*/ @MyAction("ser01")
public void ser01(){
System.out.println("call ser01");
} @MyAction("ser02")
public void ser02(){
System.out.println("call ser02");
}
}
4. Spring配置文件(APP.xml)
<context:component-scan base-package="com.spring.aop"></context:component-scan> <!-- 使 AspectJ 的注解起作用 ; autoproxy自动代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
5. 运行
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("com/spring/ioc/APP.xml");
MyServer myServer = factory.getBean(MyServer.class);
myServer.ser01();
myServer.ser02();
}

依赖jar包
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework-version}</version>
</dependency> <!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
异常:
Caused by: java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
是因为缺少 aspectjweaver 包
AOP注解式拦截的更多相关文章
- SpringBoot —— AOP注解式拦截与方法规则拦截
AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件. SpringBoot中AOP的使用 ...
- SpringBoot AOP注解式拦截与方法规则拦截
AOP的本质还是动态代理对方法调用进行增强. SpringBoot 提供了方便的注解实现自定义切面Aspect. 1.使用需要了解的几个概念: 切面.@Aspect 切点.@Pointcut. 通知. ...
- 总结切面编程AOP的注解式开发和XML式开发
有段日子没有总结东西了,因为最近确实有点忙,一直在忙于hadoop集群的搭建,磕磕碰碰现在勉强算是能呼吸了,因为这都是在自己的PC上,资源确实有点紧张(搭建过程后期奉上),今天难得大家都有空(哈哈哈~ ...
- Spring AOP实现注解式的Mybatis多数据源切换
一.为什么要使用多数据源切换? 多数据源切换是为了满足什么业务场景?正常情况下,一个微服务或者说一个WEB项目,在使用Mybatis作为数据库链接和操作框架的情况下通常只需要构建一个系统库,在该系统库 ...
- Spring AOP基础概念及自定义注解式AOP初体验
对AOP的理解开始是抽象的,看到切点的匹配方式其实与正则表达式性质大致一样就基本了解AOP是基本是个什么作用了.只是整个概念更抽象,需要具化理解.下图列表是AOP相关概念解释,可能也比较抽象^_^ 比 ...
- Spring注解式事务解析
#Spring注解式事务解析 增加一个Advisor 首先往Spring容器新增一个Advisor,BeanFactoryTransactionAttributeSourceAdvisor,它包含了T ...
- 【Spring】AOP注解方式实现机制
一.概述 二.@EnableAspectJAutoProxy 注解分析 三.分析AnnotationAwareAspectJAutoProxyCreator 四.执行流程 1. registerBea ...
- springAop:Aop(Xml)配置,Aop注解配置,spring_Aop综合案例,Aop底层原理分析
知识点梳理 课堂讲义 0)回顾Spring体系结构 Spring的两个核心:IoC和AOP 1)AOP简介 1.1)OOP开发思路 OOP规定程序开发以类为模型,一切围绕对象进行,OOP中完成某个任务 ...
- SpringMVC学习系列(9) 之 实现注解式权限验证
对大部分系统来说都需要权限管理来决定不同用户可以看到哪些内容,那么如何在Spring MVC中实现权限验证呢?当然我们可以继续使用servlet中的过滤器Filter来实现.但借助于Spring MV ...
随机推荐
- ASP.NET MVC string赋值Html格式在显示View问题总结
ViewBag.Content = "<p>你好</p>"; string 类型的赋值一个 "<h1>你好</h1>&qu ...
- 20170413 F110学习
F110 学习: Tcode: F110 自动付款业务, FBZP 维护收付程序设置 FBL1N 供应商行项目 XK03 显示供应商(银行信息维护) F110 ...
- mysql-5.7.16-linux-glibc2.5-x86_64精简后的主从配置
1.创建复制账号,并授予复制权限CREATE USER 'fansik'@'10.%' IDENTIFIED BY 'fansik';GRANT REPLICATION SLAVE ON *.* TO ...
- 前端之 Ajax(补)
概述 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用 一个简单操作需要 ...
- Linux Shell基础 Shell的输入重定向和输出重定向
概述 在 Linux 中输入设备指的是键盘,输出设备指的是显示器.在 Linux 中,所有的内容都是文件,计算机硬件也是文件,标准输入设备(键盘)和标准输出设备(显示器)也是文件.这些设备的设备文件名 ...
- mongodb简介和特性
1.mongodb是基于文档的(BSON,类似json的键值对来存储),不是基于表格,易于水平扩展,将内部相关的数据放在一起能提高数据库的操作性能.如果你想新建一个新的文档类型,不用事先告诉数据库关于 ...
- 编译android源码m、mm、mmm命令的使用
http://blog.163.com/zz_forward/blog/static/212898222201442873435471/ gcc怎么查看它的默认include路径和库的路径呢? //- ...
- Python编程-多道技术和进程
一.多道技术 1.多路复用 操作系统主要使用来 记录哪个程序使用什么资源 对资源请求进行分配 为不同的程序和用户调解互相冲突的资源请求. 我们可将上述操作系统的功能总结为: 处理来自多个程序发起的多个 ...
- PHP搜索文件夹下全部文件
搜索文件夹下全部文件 //搜索文件夹下全部文件,暂时不支持中文文件名 public function scanFile($path) { if (!is_dir($path)) return arra ...
- Python中的条件选择和循环语句
一.条件选择语句 Python中条件选择语句的关键字为:if .elif .else这三个.其基本形式如下: if condition: block elif condition: block ... ...