Spring基于XML配置AOP
目录结构:
D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\cn\edu\bjut\service\StudentService.java
package cn.edu.bjut.service; /**
* Created by N3verL4nd on 2017/3/24.
*/
public interface StudentService {
public void addStudent(String name);
}
D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\cn\edu\bjut\service\impl\StudentServiceImpl.java
package cn.edu.bjut.service.impl; import cn.edu.bjut.service.StudentService; /**
* Created by N3verL4nd on 2017/3/24.
*/
public class StudentServiceImpl implements StudentService {
@Override
public void addStudent(String name) {
System.out.println("[添加学生]:" + name);
//触发异常通知
// System.out.println( 1 / 0);
}
}
D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\cn\edu\bjut\advice\StudentServiceAspect.java
package cn.edu.bjut.advice; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; import java.util.Arrays; /**
* Created by N3verL4nd on 2017/3/24.
*/
public class StudentServiceAspect { //前置通知
public void doBefore(JoinPoint jp) {
System.out.println("--------------前置通知-------------");
System.out.println("类名:" + jp.getTarget().getClass().getName());
System.out.println("方法名:" + jp.getSignature().getName());
System.out.println("开始添加学生:" + Arrays.toString(jp.getArgs()));
System.out.println("-----------------------------------");
} //后置通知
public void doAfter(JoinPoint jp) {
System.out.println("--------------后置通知-------------");
System.out.println("学生添加完成!");
System.out.println("-----------------------------------");
} //环绕通知
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("--------------环绕通知-------------");
System.out.println("添加学生前:");
Object retVal = null;
retVal= pjp.proceed();
System.out.println("返回值:" + retVal);
System.out.println("添加学生后!");
System.out.println("-----------------------------------");
return retVal;
} //返回通知
public void doAfterReturning(JoinPoint jp) {
System.out.println("--------------返回通知-------------");
System.out.println("-----------------------------------");
} //异常通知
public void doAfterThrowing(JoinPoint jp, Throwable ex) {
System.out.println("--------------异常通知-------------");
System.out.println("异常信息:" + ex.getMessage());
System.out.println("-----------------------------------");
}
}
D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\cn\edu\bjut\test\T.java
package cn.edu.bjut.test; import cn.edu.bjut.service.StudentService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by N3verL4nd on 2017/3/24.
*/
public class T {
private ApplicationContext context = null; @Before
public void setUp() {
context = new ClassPathXmlApplicationContext("beans.xml");
} @Test
public void test() {
StudentService studentService = (StudentService) context.getBean("studentService");
studentService.addStudent("lgh");
}
}
D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\beans.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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="studentServiceAspect" class="cn.edu.bjut.advice.StudentServiceAspect"/> <bean id="studentService" class="cn.edu.bjut.service.impl.StudentServiceImpl"/> <aop:config>
<aop:aspect ref="studentServiceAspect">
<aop:pointcut id="businessService" expression="execution(public void cn.edu.bjut.service.impl.StudentServiceImpl.addStudent(String))"/>
<aop:before method="doBefore" pointcut-ref="businessService"/>
<aop:after method="doAfter" pointcut-ref="businessService"/>
<aop:around method="doAround" pointcut-ref="businessService"/>
<aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>
<aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>
</aop:aspect>
</aop:config>
</beans>
输出:
--------------前置通知-------------
类名:cn.edu.bjut.service.impl.StudentServiceImpl
方法名:addStudent
开始添加学生:[lgh]
-----------------------------------
--------------环绕通知-------------
添加学生前:
[添加学生]:lgh
--------------返回通知-------------
-----------------------------------
返回值:null
添加学生后!
-----------------------------------
--------------后置通知-------------
学生添加完成!
-----------------------------------
介绍一下org/aspectj/lang/JoinPoint.java
AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点对象,该类是JoinPoint的子接口。
任何一个增强方法都可以通过将第一个入参声明为JoinPoint访问到连接点上下文的信息。我们先来了解一下这两个接口的主要方法:
1)JoinPoint
java.lang.Object[] getArgs():获取连接点方法运行时的入参列表;
Signature getSignature() :获取连接点的方法签名对象;
java.lang.Object getTarget() :获取连接点所在的目标对象;
java.lang.Object getThis() :获取代理对象本身;
2)ProceedingJoinPoint
ProceedingJoinPoint继承JoinPoint子接口,它新增了两个用于执行连接点方法的方法:
java.lang.Object proceed() throws java.lang.Throwable:通过反射执行目标对象的连接点处的方法;
java.lang.Object proceed(java.lang.Object[] args) throws java.lang.Throwable:通过反射执行目标对象连接点处的方法,不过使用新的入参替换原来的入参。
Spring基于XML配置AOP的更多相关文章
- Spring 基于XML配置
基于XML的配置 对于基于XML的配置,Spring 1.0的配置文件采用DTD格式,Spring2.0以后采用Schema格式,后者让不同类型的配罝拥有了自己的命名空间,使得配置文件更具扩展性.此外 ...
- Spring 基于xml配置方式的AOP
我们具体用代码来说明: 1.ArithmeticCalculator.java package com.proc; public interface ArithmeticCalculator { in ...
- Spring 基于xml配置方式的AOP(8)
1.ArithmeticCalculator.java 1 package com.proc; 2 3 public interface ArithmeticCalculator { 4 int ad ...
- Spring 使用xml配置aop
1.xml文件需要引入aop命名空间 2.xml内容: <?xml version="1.0" encoding="UTF-8"?> <bea ...
- Spring 基于xml配置方式的事务
参考前面的声明式事务的例子:http://www.cnblogs.com/caoyc/p/5632198.html 我们做了相应的修改.在dao中和service中的各个类中,去掉所有注解标签.然后为 ...
- Spring 基于xml配置方式的事务(14)
参考前面的声明式事务的例子:http://www.cnblogs.com/caoyc/p/5632198.html 我们做了相应的修改.在dao中和service中的各个类中,去掉所有注解标签.然后为 ...
- Spring基于注解配置AOP
D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\aop.xml <?xml version="1.0" encoding ...
- Spring、XML配置AOP
新建一个AOP类: public class MyInterceptor2 { public void doAccessCheck(){ System.out.println("前置通知 & ...
- 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP
上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...
随机推荐
- JVM探秘:垃圾收集器
本系列笔记主要基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版>,是这本书的读书笔记. 垃圾收集器 垃圾收集算法是是内存回收的方法论,垃圾收集器是内存回收的具体实现.不同的虚 ...
- 日期格式化使用 YYYY-MM-dd 的潜在问题
昨天在v站上看到这个关于YYYY-MM-dd的使用而出现Bug的帖子(v2ex.com/t/633650)非常有意思,所以拿过来分享一下. 在任何编程语言中,对于时间.数字等数据上,都存在很多类似这种 ...
- 1071 小赌怡情 (15分)C语言
常言道"小赌怡情".这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大还是小:玩家下注 t 个筹码后,计算机给出第二个数.若玩家猜对了,则 ...
- 1034 有理数四则运算 (20 分)C语言
题目描述 本题要求编写程序,计算2个有理数的和.差.积.商. 输入描述: 输入在一行中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整 ...
- (openssh、telnet、vsftpd、nfs、rsync、inotify、samba)
(openssh.telnet.vsftpd.nfs.rsync.inotify.samba) 一:OpenSSH服务与Telnet服务(必须掌握) 前言:OpenSSH是加密传输,Telnet是明文 ...
- nodeJS实现识别验证码(tesseract-ocr+GraphicsMagick)
背景 最近在写一个爬虫的小工具,卡在登录这里. 想爬的网站需要登录才能获取数据,登录又需要输入验证码. 好在验证码是简单的验证码,还可以自己识别试试. 需求分析 1.保存验证码图片 2.识别验证码 3 ...
- KafkaProducer Sender 线程详解(含详细的执行流程图)
目录 1.Sender 线程详解 2.RecordAccumulator 核心方法详解 温馨提示:本文基于 Kafka 2.2.1 版本. 上文 <源码分析 Kafka 消息发送流程> 已 ...
- centos7+docker+elasticsearch 安装记录+踩坑
版本: cenos7 :3.10.0-957.21.3.el7.x86_64 (内核需>=3.10 才可以安装) docker: yum安装版本为1.13.1 elasticsearch: 6 ...
- 程序员如何才能跨过高级级别,譬如腾讯T3.1/阿里P7
首先自我介绍下自己履历:5年前过了腾讯的T3.2,最近又在1年多前过了阿里的P8,目前在B站. **腾讯** 在腾讯我是T2.1社招一般水平入职的,3年后到了T3.2.中间是经历过几个转变:刚来的半年 ...
- Spring学习记录5——数据库事务基础知识
何为数据库事务 “一荣共荣,一损共损”这句话很能体现事务的思想,很多复杂的事务要分步进行,但它们组成了一个整体,要么整体生效,要么整体失效.这种思想反映到数据库上,就是多条SQL语句,要么全部成功,要 ...