基于注解的Spring AOP示例
基于注解的Spring AOP示例
在XML配置文件中开启 @AspectJ 支持
要使用Spring的AOP,首先要在 applicationContext.xml 配置文件中添加如下内容:
<!-- 启动@Aspectj -->
<aop:aspectj-autoproxy/>
声明切面及切入点
在Spring中, 切面 就是使用 @Aspect 注解的类。而 切入点 则由两部分组成:方法签名和切入点表达式。下面的切面中声明了三种切入点。
@Aspect
public class SampleAspect {
// 匹配所有的公共方法
@Pointcut("execution(public * *(..))")
public void publicMethod() {
} // 匹配所有在com.app.bean包中的方法
@Pointcut("within(com.app.bean.*)")
public void inPackage() {
} // 匹配所有带有CustomAnnotation注解的方法
@Pointcut("@annotation(com.app.annotation.CustomAnnotation)")
public void withAnnotation() {
}
}
其中第三个切入点使用了自定义的注解类 CustomAnnotation 。
public @interface CustomAnnotation {}
声明通知
接下来要声明的是 通知 ,通知与切面的结构基本一致。不同的是通知使用 @Before , @Around 等注解同切面中的切入点一起确定执行的方法。下面的通知示例中声明了三种通知方式,其中 @Around 类型的通知需要一个 ProceedingJoinPoint 类的实例作为参数。
@Aspect
public class SampleAdvice {
@Before("com.app.aspect.SampleAspect.publicMethod()")
public void advicePublicMethod() {
System.out.println("before advice matched with public methods");
} @After("com.app.aspect.SampleAspect.inPackage()")
public void adviceInBean() {
System.out.println("after advice matched methods in package");
} @Around("com.app.aspect.SampleAspect.withAnnotation()")
public Object adviceWithAnnotation(ProceedingJoinPoint jp) throws Throwable {
System.out.println("around advice before proceed");
Object ret = jp.proceed();
System.out.print("around advice after proceed");
return ret;
}
}
之前声明的切面和通知都需要在配置文件中声明一下,最终的 applicationContext.xml 如下所示:
<?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"
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">
<context:component-scan base-package="com.app"/>
<!-- 启动@Aspectj -->
<aop:aspectj-autoproxy/> <!-- 切面 -->
<bean id="sampleAspect" class="com.app.aspect.SampleAspect"/>
<!-- 通知 -->
<bean id="sampleAdvice" class="com.app.aspect.SampleAdvice"/>
</beans>
测试
现在可以写一个普通的类来测试我们的代码了,在 com.app.bean 包中创建文件:
@Component
public class CommonService {
public void service() {
System.out.println("service method");
} public void transfer() {
System.out.println("transfer method");
} @CustomAnnotation
public void annotated() {
System.out.println("method with annotation");
}
}
测试代码如下:
public class Test {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
CommonService service = context.getBean("commonService", CommonService.class);
service.service();
service.transfer();
service.annotated();
}
}
结语
本文中的示例代码是基于Maven项目的,最终代码的包结构如下:

pom.xml 文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.eagle</groupId>
<artifactId>springDemo</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
基于注解的Spring AOP示例的更多相关文章
- 基于注解的Spring AOP的配置和使用
摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...
- 基于注解的Spring AOP的配置和使用--转载
AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...
- 基于注解的Spring AOP入门、增强Advice实例
这篇文章简单通过一个例子,介绍几种增强的基本配置,以方便spring框架初学者对aop的代码结构有个清楚的了解认识.首先,spring支持aop编程,支持aspectJ的语法格式来表示切入点,切面,增 ...
- 基于注解的Spring AOP拦截含有泛型的DAO
出错场景 1.抽象类BaseDao public abstract class BaseDao<T> { public BaseDao() { entityClass = (Class&l ...
- Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现
转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...
- Spring:基于注解的Spring MVC
什么是Spring MVC Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...
- 基于注解的Spring多数据源配置和使用(非事务)
原文:基于注解的Spring多数据源配置和使用 1.创建DynamicDataSource类,继承AbstractRoutingDataSource package com.rps.dataSourc ...
- Spring7:基于注解的Spring MVC(下篇)
Model 上一篇文章<Spring6:基于注解的Spring MVC(上篇)>,讲了Spring MVC环境搭建.@RequestMapping以及参数绑定,这是Spring MVC中最 ...
- Spring系列9:基于注解的Spring容器配置
写在前面 前面几篇中我们说过,Spring容器支持3种方式进行bean定义信息的配置,现在具体说明下: XML:bean的定义和依赖都在xml文件中配置,比较繁杂. Annotation-based ...
随机推荐
- SVN图形管理工具-Submint
1.安装svn及相关依赖包 yum install subversion httpd mod_dav_svn apr-util-sqlite 2.下载submin wget https://sup ...
- MFC动态创建菜单
http://blog.csdn.net/csdnzhwk/article/details/47395639
- 服务器断电保护神v2.2
下载链接: https://pan.baidu.com/s/1bph5IFX 密码: evbn 使用说明:①每15秒检测一次,当列表内的主机PING不通时将执行关机②支持4种关机方式,理论上第一种方式 ...
- HDU1760 A New Tetris Game NP态
A New Tetris Game Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 解决小米、红米及其他 Android 手机无法在 Mac 下进行真机调试的问题(转)
转自:http://ju.outofmemory.cn/entry/103522 Begin iOS 2014-08-19 271 阅读 手机 Android 小米 mac 调试 在 Mac OSX ...
- html5本地存储(localStorage)使用介绍
1.html5几种存储形式 本地存储(localStorage && sessionStorage) 离线缓存(application cache) indexedDB 和 webSQ ...
- JavaScript中的数组遍历forEach()与map()方法以及兼容写法
原理: 高级浏览器支持forEach方法 语法:forEach和map都支持2个参数:一个是回调函数(item,index,list)和上下文: forEach:用来遍历数组中的每一项:这个方法执行是 ...
- Javascript 数组常用操作方法
一.数组 Array 1.创建数组 /* 构造函数 */ var arr1 = new Array(); //创建一个空数组 var arr1 = new Array(5); //创建指定长度数组(数 ...
- Nginx基础知识之————RTMP模块中的中HLS专题(翻译文档)
一.在Nginx配置文件的RTMP模块中配置hls hls_key_path /tmp/hlskeys; 提示错误信息: nginx: [emerg] the same path name " ...
- Too Many Connections: How to Increase the MySQL Connection Count To Avoid This Problem
1.问题描述 在启动使用mysql数据库的项目时,遇到一个报错,如下: Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConn ...