目录结构:

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的更多相关文章

  1. Spring 基于XML配置

    基于XML的配置 对于基于XML的配置,Spring 1.0的配置文件采用DTD格式,Spring2.0以后采用Schema格式,后者让不同类型的配罝拥有了自己的命名空间,使得配置文件更具扩展性.此外 ...

  2. Spring 基于xml配置方式的AOP

    我们具体用代码来说明: 1.ArithmeticCalculator.java package com.proc; public interface ArithmeticCalculator { in ...

  3. Spring 基于xml配置方式的AOP(8)

    1.ArithmeticCalculator.java 1 package com.proc; 2 3 public interface ArithmeticCalculator { 4 int ad ...

  4. Spring 使用xml配置aop

    1.xml文件需要引入aop命名空间 2.xml内容: <?xml version="1.0" encoding="UTF-8"?> <bea ...

  5. Spring 基于xml配置方式的事务

    参考前面的声明式事务的例子:http://www.cnblogs.com/caoyc/p/5632198.html 我们做了相应的修改.在dao中和service中的各个类中,去掉所有注解标签.然后为 ...

  6. Spring 基于xml配置方式的事务(14)

    参考前面的声明式事务的例子:http://www.cnblogs.com/caoyc/p/5632198.html 我们做了相应的修改.在dao中和service中的各个类中,去掉所有注解标签.然后为 ...

  7. Spring基于注解配置AOP

    D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\aop.xml <?xml version="1.0" encoding ...

  8. Spring、XML配置AOP

    新建一个AOP类: public class MyInterceptor2 { public void doAccessCheck(){ System.out.println("前置通知 & ...

  9. 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP

    上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...

随机推荐

  1. linux MySQL 5.7.20安装教程

    安装MySQL 5.7.20shell> cd /usr/localshell> groupadd mysqlshell> useradd -g mysql mysqlshell&g ...

  2. 浅析vue封装自定义插件

    在使用vue的过程中,经常会用到Vue.use,但是大部分对它一知半解,不了解在调用的时候具体做了什么,因此,本文简要概述下在vue中,如何封装自定义插件. 在开始之前,先补充一句,其实利用vue封装 ...

  3. Good Bye 2019(前五题题解)

    这套也是后来补得. 我太菜了,第三题就卡着了.想了好久才做出来,要是参加了绝对掉分. D题是人生中做完的第一道交互题,不容易. 比赛传送门 A.Card Game 题目大意:一共有n张互不相同的牌,玩 ...

  4. Go并发编程

    概述 简而言之,所谓并发编程是指在一台处理器上"同时"处理多个任务. 随着硬件的发展,并发程序变得越来越重要.Web服务器会一次处理成千上万的请求.平板电脑和手机app在渲染用户画 ...

  5. SCU 4439 Vertex Cover|最小点覆盖

    传送门 Vertex Cover frog has a graph with n vertices v(1),v(2),…,v(n)v(1),v(2),…,v(n) and m edges (v(a1 ...

  6. 通过例子学习C++(二)最小公倍数

    本文是通过例子学习C++的第二篇,通过这个例子可以快速入门c++相关的语法. 题目要求:输入两个整数,求其最小公倍数. 解答方法一:两个数的最小公倍数,是这两个数中的大数,或者是这2个数的倍数中的最小 ...

  7. MySQL数据库(三)

    前提要述:参考书籍<MySQL必知必会> 2.1 MySQL简介 2.1.1 什么是MySQL MySQL是一种关系数据库管理系统.负责数据库中数据的存储,检索,管理和处理. 2.1.2 ...

  8. 【转】[IT综合面试]牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结

    感谢IT面试群 S-北京-陈磊 的整理分享.   基础篇:操作系统.计算机网络.设计模式         提高篇:WIN32.MFC与Linux 算法篇:算法与数据结构           一:操作系 ...

  9. JS怎样做四舍五入

    1 .tofixed方法 toFixed() 方法可把 Number 四舍五入为指定小数位数的数字.例如将数据Num保留2位小数,则表示为:toFixed(Num):但是其四舍五入的规则与数学中的规则 ...

  10. 一款精美的Toast第三方库的简单使用

    以前一直用的安卓原生Toast,个人感觉Toast这东西,没必要花功夫,知道看到了Toasty这东西,立刻被圈粉了,真的非常好看. 项目地址 我们都知道,安卓原生Toast的用法是 Toast.mak ...