基于注解的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示例的更多相关文章

  1. 基于注解的Spring AOP的配置和使用

    摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...

  2. 基于注解的Spring AOP的配置和使用--转载

    AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  3. 基于注解的Spring AOP入门、增强Advice实例

    这篇文章简单通过一个例子,介绍几种增强的基本配置,以方便spring框架初学者对aop的代码结构有个清楚的了解认识.首先,spring支持aop编程,支持aspectJ的语法格式来表示切入点,切面,增 ...

  4. 基于注解的Spring AOP拦截含有泛型的DAO

    出错场景 1.抽象类BaseDao public abstract class BaseDao<T> { public BaseDao() { entityClass = (Class&l ...

  5. Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现

    转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...

  6. Spring:基于注解的Spring MVC

    什么是Spring MVC Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...

  7. 基于注解的Spring多数据源配置和使用(非事务)

    原文:基于注解的Spring多数据源配置和使用 1.创建DynamicDataSource类,继承AbstractRoutingDataSource package com.rps.dataSourc ...

  8. Spring7:基于注解的Spring MVC(下篇)

    Model 上一篇文章<Spring6:基于注解的Spring MVC(上篇)>,讲了Spring MVC环境搭建.@RequestMapping以及参数绑定,这是Spring MVC中最 ...

  9. Spring系列9:基于注解的Spring容器配置

    写在前面 前面几篇中我们说过,Spring容器支持3种方式进行bean定义信息的配置,现在具体说明下: XML:bean的定义和依赖都在xml文件中配置,比较繁杂. Annotation-based ...

随机推荐

  1. webstorm运行到服务器(Apache)

    昨天百度了很多关于webstorm怎么运行到服务器当中的例子,但是我都阅读了一遍,里边貌似没有是关于Apache跟webstorm的配置方式.所以下面是我给大家分享我的亲身体验. 再次强调:这里用的8 ...

  2. Android 进程常驻----native保活5.0以上方案推演过程以及代码

    正文: 上一篇我们通过父子进程间建立双管道,来监听进程死掉,经过测试,无耗电问题,无内存消耗问题,可以在设置中force close下成功拉起,也可以在获取到root权限的360/cleanmaste ...

  3. 2016年12月31日 星期六 --出埃及记 Exodus 21:26

    2016年12月31日 星期六 --出埃及记 Exodus 21:26 "If a man hits a manservant or maidservant in the eye and d ...

  4. window7 x64 path

    %SystemRoot%\system32; %SystemRoot%; %SystemRoot%\System32\Wbem; %SYSTEMROOT%\System32\WindowsPowerS ...

  5. Android 秒级编译 Freeline

    http://mp.weixin.qq.com/s?__biz=MzA4NTQwNDcyMA==&mid=2650662410&idx=1&sn=c654fa7b0cc8c91 ...

  6. apache域名本地映射

    A: 第一步(搜索allow) 第二步(搜索vhost) 第三步(搜索rewrite) B: C:

  7. 如何修改Hadoop的默认日志级别,还真是麻烦

    鄙人使用的Hadoop版本为2.6.4.Hadoop的默认日志级别为INFO,对于百台以上的集群,如果文件操作频繁的话,NameNode会狂打日志,对性能会有一定的影响. 我们可以通过http://& ...

  8. DbContext 那些事 —— 数据库初始化

    数据库初始化 上图,这个图解释了,数据库初始化的流程,是基于我们在上下文类中的构造函数中传递的参数. 在上面的图中,context类中的base构造器中,可以填入下面的参数: 无参数(No Param ...

  9. 【转】关于 mate viewport属性的具体说明!

    什么是Viewport 手机浏览器是把页面放在一个虚拟的"窗口"(viewport)中,通常这个虚拟的"窗口"(viewport)比屏幕宽,这样就不用把每个网页 ...

  10. RSA3:预提取数据

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...