基于注解的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. 模板——RMQ

    就是模板 #include <cstdio> #include <cstring> #include <iostream> using namespace std; ...

  2. SQL Server 2008 中收缩数据库(DUMP,TRANSACTION,TRAN,无效,语法错误)

    从SQL SERVER 2008 开始,我们已经不能再用以前 DUMP TRAN 数据库名 WITH NO_LOG 的这种方式来收缩数据库,但是,可以用另外一种替代的方法,SQL语句如下: ALTER ...

  3. freemaker时间格式转换,精确到毫秒

    在开发中,需要将时间以 2016-09-20 12:00:01:723 的形式里用freemaker展示在页面上,找了好久,终于找到了答案. "createTime":" ...

  4. 名词王国里的死刑execution in the kingdom of nouns

    http://www.cnblogs.com/bigfish--/archive/2011/12/31/2308407.htmlhttp://justjavac.com/java/2012/07/23 ...

  5. Python库安装

    标签(空格分隔): Python 安装pip 正如管理Java Project的Maven,管理Scalar Project的sbt一样,Python也有其管理工具 -- pip,也是今天我们的主角哦 ...

  6. 搞清css的单位 px,em,rem的区别

    前言:现在上大街一眼望去,基本上90%的人都拿着手机,走路,逛街,吃东西都不停着,所以对于我们这种前端开发的程序猿来说,让网页适应于移动端可以说是必须要满足的.所以最近也是一直在学习和实践.然后就接触 ...

  7. WCF入门简单教程(图文) VS2010版

    在这个例子中我们将使用VS 2010 创建一个WCF服务,其中会了解 [DataContract] [ServiceContract] 等特性.  内置的 WCFSVCHost ,并使用“WCF测试客 ...

  8. session失效

    今天写页面时,使用了session 设置session后 跳转页面session就丢失 上网查了一下也没有很好的解决办法 也没有说原因. 在自己本地电脑上写了两个页面测试一下,完全正常.但是上传到服务 ...

  9. 承接unity外包:2016年VR产业八大发展趋势

    在上周进行的2016年全球游戏开发者大会(GDC)期间,虚拟现实技术是一个重要议题.英文科技媒体VentureBeat近日刊出了一篇文章,对2016年VR产业的发展趋势进行了预测.游戏陀螺对文章分享的 ...

  10. DataTable转换为Json字符串的三种方法

    //第一种:使用StringBuilder  public string DataTableToJson(DataTable table) { var JsonString = new StringB ...