Spring学习(十八)----- Spring AOP+AspectJ注解实例
我们将向你展示如何将AspectJ注解集成到Spring AOP框架。在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法。
- @Before – 方法执行前运行
- @After – 运行在方法返回结果后
- @AfterReturning – 运行在方法返回一个结果后,在拦截器返回结果。
- @AfterThrowing – 运行方法在抛出异常后,
- @Around – 围绕方法执行运行,结合以上这三个通知。
1. 目录结构
2. Spring Beans
package com.yiibai.customer.bo;
public interface CustomerBo {
void addCustomer();
String addCustomerReturnValue();
void addCustomerThrowException() throws Exception;
void addCustomerAround(String name);
}
package com.yiibai.customer.bo.impl;
import com.yiibai.customer.bo.CustomerBo;
public class CustomerBoImpl implements CustomerBo {
public void addCustomer(){
System.out.println("addCustomer() is running ");
}
public String addCustomerReturnValue(){
System.out.println("addCustomerReturnValue() is running ");
return "abc";
}
public void addCustomerThrowException() throws Exception {
System.out.println("addCustomerThrowException() is running ");
throw new Exception("Generic Error");
}
public void addCustomerAround(String name){
System.out.println("addCustomerAround() is running, args : " + name);
}
}
4. 启用AspectJ
File : applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <aop:aspectj-autoproxy /> <bean id="customerBo" class="com.yiibai.customer.bo.impl.CustomerBoImpl" /> <!-- Aspect -->
<bean id="logAspect" class="com.yiibai.aspect.LoggingAspect" /> </beans>
4. AspectJ @Before
File : LoggingAspect.java
package com.yiibai.aspect; import org.aspectj.lang.JoinYiibai;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; @Aspect
public class LoggingAspect { @Before("execution(* com.yiibai.customer.bo.CustomerBo.addCustomer(..))")
public void logBefore(JoinYiibai joinYiibai) { System.out.println("logBefore() is running!");
System.out.println("hijacked : " + joinYiibai.getSignature().getName());
System.out.println("******");
} }
运行
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
customer.addCustomer();
输出结果
logBefore() is running!
hijacked : addCustomer
******
addCustomer() is running
5. AspectJ @After
File : LoggingAspect.java
package com.yiibai.aspect; import org.aspectj.lang.JoinYiibai;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.After; @Aspect
public class LoggingAspect { @After("execution(* com.yiibai.customer.bo.CustomerBo.addCustomer(..))")
public void logAfter(JoinYiibai joinYiibai) { System.out.println("logAfter() is running!");
System.out.println("hijacked : " + joinYiibai.getSignature().getName());
System.out.println("******"); } }
运行它
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
customer.addCustomer();
输出结果
addCustomer() is running
logAfter() is running!
hijacked : addCustomer
******
6. AspectJ @AfterReturning
在下面例子中,logAfterReturning()方法将在 customerBo 接口的addCustomerReturnValue()方法执行之后执行。此外,还可以截取返回的值使用“returning”属性。
File : LoggingAspect.java
package com.yiibai.aspect; import org.aspectj.lang.JoinYiibai;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterReturning; @Aspect
public class LoggingAspect { @AfterReturning(
pointcut = "execution(* com.yiibai.customer.bo.CustomerBo.addCustomerReturnValue(..))",
returning= "result")
public void logAfterReturning(JoinYiibai joinYiibai, Object result) { System.out.println("logAfterReturning() is running!");
System.out.println("hijacked : " + joinYiibai.getSignature().getName());
System.out.println("Method returned value is : " + result);
System.out.println("******");
}
}
运行它
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
customer.addCustomerReturnValue();
输出结果
addCustomerReturnValue() is running
logAfterReturning() is running!
hijacked : addCustomerReturnValue
Method returned value is : abc
******
7. AspectJ @AfterReturning
File : LoggingAspect.java
package com.yiibai.aspect; import org.aspectj.lang.JoinYiibai;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterThrowing; @Aspect
public class LoggingAspect { @AfterThrowing(
pointcut = "execution(* com.yiibai.customer.bo.CustomerBo.addCustomerThrowException(..))",
throwing= "error")
public void logAfterThrowing(JoinYiibai joinYiibai, Throwable error) { System.out.println("logAfterThrowing() is running!");
System.out.println("hijacked : " + joinYiibai.getSignature().getName());
System.out.println("Exception : " + error);
System.out.println("******"); }
}
运行它
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
customer.addCustomerThrowException();
输出结果
addCustomerThrowException() is running
logAfterThrowing() is running!
hijacked : addCustomerThrowException
Exception : java.lang.Exception: Generic Error
******
Exception in thread "main" java.lang.Exception: Generic Error
//...
8. AspectJ @Around
在下面例子中,logAround()方法将在customerBo接口的addCustomerAround()方法执行之前执行, 必须定义“joinYiibai.proceed();” 控制何时拦截器返回控制到原来的addCustomerAround()方法。
File : LoggingAspect.java
package com.yiibai.aspect; import org.aspectj.lang.ProceedingJoinYiibai;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around; @Aspect
public class LoggingAspect { @Around("execution(* com.yiibai.customer.bo.CustomerBo.addCustomerAround(..))")
public void logAround(ProceedingJoinYiibai joinYiibai) throws Throwable { System.out.println("logAround() is running!");
System.out.println("hijacked method : " + joinYiibai.getSignature().getName());
System.out.println("hijacked arguments : " + Arrays.toString(joinYiibai.getArgs())); System.out.println("Around before is running!");
joinYiibai.proceed(); //continue on the intercepted method
System.out.println("Around after is running!"); System.out.println("******"); } }
运行它
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo");
customer.addCustomerAround("yiibai");
输出结果
logAround() is running!
hijacked method : addCustomerAround
hijacked arguments : [yiibai]
Around before is running!
addCustomerAround() is running, args : yiibai
Around after is running!
******
总结
Spring学习(十八)----- Spring AOP+AspectJ注解实例的更多相关文章
- spring学习 十八 spring的声明事物
1.编程式事务: 1.1 由程序员编程事务控制代码.commit与rollback都需要程序员决定在哪里调用,例如jdbc中conn.setAutoCimmit(false),conn.commit( ...
- Spring学习十四----------Spring AOP实例
© 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...
- Spring学习(八)AOP详解
文章更新时间:2020/04/06 一.一个例子 在上面的例子中,包租婆的核心业务就是签合同,收房租,那么这就够了,灰色框起来的部分都是重复且边缘的事,交给中介商就好了,这就是 AOP 的一个思想:让 ...
- Spring学习(十六)----- Spring AOP实例(Pointcut(切点),Advisor)
在上一个Spring AOP通知的例子,一个类的整个方法被自动拦截.但在大多数情况下,可能只需要一种方式来拦截一个或两个方法,这就是为什么引入'切入点'的原因.它允许你通过它的方法名来拦截方法.另外, ...
- Spring学习(十五)----- Spring AOP通知实例 – Advice
Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...
- Spring学习十五----------Spring AOP API的Pointcut、advice及 ProxyFactoryBean相关内容
© 版权声明:本文为博主原创文章,转载请注明出处 实例: 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4. ...
- Spring学习(十九)----- Spring的五种事务配置详解
前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ...
- spring学习 十六 spring加载属性文件
第一步:创建一个properties文件,以数据库链接作为实例db.properties jdbc.url=jdbc:mysql://192.168.153.128:3306/mybaties?cha ...
- spring学习 十五 spring的自动注入
一 :在 Spring 配置文件中对象名和 ref=”id” ,id 名相同使用自动注入,可以不配置<property/>,对应的注解@Autowired的作用 二: 两种配置办法 (1 ...
随机推荐
- 解决华为交换机S5700无法解除ip/Mac绑定的问题
今天同事离职,需要解除他的个人笔记本Mac与ip的绑定 首先进入系统用户视图,然后进入vlanif4,解除151绑定 system-view interface vlanif 4 undo dhcp ...
- jQuery插件实例三:图片滚动[切换]效果一
图片切换效果在很多网站上都能看到,是一种常见的广告/活动宣传方式,通常位于网页上端.这个插件是众多图片切换效果的形式中的一种,数据源可在前端配置,也可从后台通JSON格式传输数据,当然,数据格式是固定 ...
- C++ Boost在Windows和Linux下的编译安装
再debian下直接apt-get install gcc g++就可以了.按照类似的逻辑,再Fedora下yum install gcc g++ 报告无法找到g++包. 差了一下,原来这个包的名字叫 ...
- 【C#】#101 导入导出Excel
项目需求: 1.把数据导出到Excel: 2.把Excel数据导入到数据库 使用的类库: Aspose.Cells ExcelHelper.zip下载 一.导出[调用已经封装好的方法][未 ...
- tomcat6 集群配置
1. 概要 web容器在做集群配置时,有3点需要注意: 1.1. 负载均衡配置: 1.2. session共享: 1.3. 若做的是单机集群(多个tomcat安装在同一台机器上),需要注意端口冲突问题 ...
- 使用Yarn+Webpack+Babel6搭建React.js环境
使用Yarn+Webpack+Babel6搭建React.js环境 Facebook开源的React.js已经改变了世人对前端UI的思考方式.这种基于组件方式的优势之一,就是使View更加的简单,因为 ...
- 使用redis4.0.1和redis-cluster搭建集群并编写重启shell脚本
1.删除机器上原有的redis2.8 关闭redis-server killall -9 redis-server 查找redis文件所在目录 which redis 删除相关文件 rm -rf re ...
- memcached迁移方案——记一次memcached session服务的迁移
背景: (1)由于机房调整,需要迁移memcached: (2)需要在短期内迁移完成(一周以内): (3)该memcached 保存了用户的登录数据,非常重要,一旦出问题将导致大量的用户被踢出: (4 ...
- Kafka设计解析(十三)Kafka消费组(consumer group)
转载自 huxihx,原文链接 Kafka消费组(consumer group) 一直以来都想写一点关于kafka consumer的东西,特别是关于新版consumer的中文资料很少.最近Kafka ...
- CMD centos7 安装 最新版本的docker -- dockerfire 原语 ENTRYPOINT - 导入镜像 tar mariadb Dockerfile 构建镜像
yum update # vim /etc/yum.repos.d/docker.repo //添加以下内容 [dockerrepo] name=Docker Repository baseurl=h ...