目录结构:

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. selenium模块的基本使用

    一.selenium库与requests库的区别 - selenium请求库: - 本质上是一个自动化测试模块; ---> 主要用于测试 UI界面 - selenium除了可以做自动化测试,还可 ...

  2. 开发工具篇:JAVA和IntelliJ IDEA相恋

    开发工具篇:JAVA和IntelliJ IDEA相恋 idea是什么? IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之 ...

  3. 我的代码真的没有bug,稍等,先试试小黄鸭调试法

    今天测试同学为了赶进度,加班去测试我的功能. 因为我的代码都写完了,也没有陪测的必要,所以就没去了~ 下午第一个问题提过来,根据经验,这个应该是测试的逻辑问题,最后他自己也发现了. 过了一会,提了第二 ...

  4. 洛谷P1832 A+B Problem(再升级) 题解 完全背包方案计数

    题目链接:https://www.luogu.com.cn/problem/P1832 题目大意: 给定一个正整数n,求将其分解成若干个素数之和的方案总数. 解题思路: 首先找到所有 \(\le n\ ...

  5. 「Luogu P1210」回文检测 解题报告

    题面 这是一道诡异的黄题 居然让你求一串吧啦吧啦的东西中 字母(大小写)最长的回文串的长度,还要输出完整的串 吐血 思路: 保持淡定,我们啥都不会,就会Manacher,那就用Manacher大法! ...

  6. Go中的Package和Module分析

    Package 所谓package(包)其实就是代码的一种组织管理方式,代码多了就需要放入文件,文件多了就需要归类放入文件夹,就好比我们在给电脑装软件时会进行归类安装,其实也是有意无意对电脑软件安装的 ...

  7. java小项目之:象棋,羡慕你们有对象的!

    象棋,是我国传统棋类益智游戏,在中国有着悠久的历史,属于二人对抗性游戏的一种,由于用具简单,趣味性强,成为流行极为广泛的棋艺活动.中国象棋是中国棋文化也是中华民族的文化瑰宝. 象棋还有很多口诀,这是最 ...

  8. 小白学 Python 爬虫(38):爬虫框架 Scrapy 入门基础(六) Item Pipeline

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  9. XSS基础学习

    XSS基础学习 By:Mirror王宇阳 什么是XSS XSS攻击是指在网页中嵌入一段恶意的客户端Js脚本代码片段,JS脚本恶意代码可以获取用户的Cookie.URL跳转.内容篡改.会话劫持--等. ...

  10. 基于 HTML5 WebGL 构建 3D 智能数字化城市全景

    前言 自 2011 年我国城镇化率首次突破 50% 以来,<新型城镇化发展规划>将智慧城市列为我国城市发展的三大目标之一,并提出到 2020 年,建成一批特色鲜明的智慧城市.截至现今,全国 ...