基于注解的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 ...
随机推荐
- XML序列化
1.先创建资产文件assets AssetManager am=getAssts(); 2.获得文件字节输入流 InputStrem is=am.open("weather"); ...
- python 学习笔记十九 django深入学习四 cookie,session
缓存 一个动态网站的基本权衡点就是,它是动态的. 每次用户请求一个页面,Web服务器将进行所有涵盖数据库查询到模版渲染到业务逻辑的请求,用来创建浏览者需要的页面.当程序访问量大时,耗时必然会更加明显, ...
- git 设置不需要输入密码
https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密码的困扰而且又享受https带来的极速 设置记住密码(默认15分钟): git config --global credenti ...
- google浏览器打开报出文件不可读解决方案
1.打开*.desktop文件 gedit ~/.local/share/applications/name.desktop 在文件中做改动: The .desktop file should loo ...
- 第一次接触OOM
前几天机器上一直遇到cpu100%,负载很高,经常报out of memory. 今天机器又遇到了,感觉这个东西无从下手,内存不够,tree看了下cache是不是太多了. 清理了一下,其实占用的不是特 ...
- CocoaPods使用 主要带图。转载。
原文地址 快速更新,不需要查找更新库 命令:pod update --verbose --no-repo-update 虽然网上关于CocoaPods安装教程多不胜数,但是我在安装的过程中还是出现了 ...
- JAVA 1.7 流程控制语句 续
1. Java中的循环控制语句一共有3种,分别是while,do… while以及for循环.2. while循环,形式为:while(布尔表达式){//待执行的代码}3. do…while循环,新式 ...
- vc6.0如何显示行号以及出现版本不兼容问题
有时编译时,提示某某行有错,但是要定位到某一行的话,如果在编辑页面能够将行号显示出来,查找也就更方便了,下面我来介绍一下让VC6.0显示行号的方法. 工具/原料 VC6.0.显示行号的插件 方 ...
- 对象排序,compareTo
第一个例子 /*为了比较,让自己的类实现Comparable接口,按照自己想要的排序方式重写compareTo *Map只是提供了对键的排序,但是当我们需要对值排序时就的提供我们自己的比较器 这里 只 ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...